...

View Full Version : static keyword



timgolding
02-16-2012, 10:07 PM
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.

Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.

Static properties cannot be accessed through the object using the arrow operator ->.

Calling non-static methods statically generates an E_STRICT level warning.




So now i am learning about the keyword static. I read this on php.net and set about testing some of these statments.

i make a simple class



class Foo {
public static function aStaticMethod() {
// ...
echo "hello";
}
}

Foo::aStaticMethod();



I then take away the static keyword from the code



error_reporting(E_ALL);
class Foo {
public function aStaticMethod() {
// ...
echo "hello";
}
}

Foo::aStaticMethod();


and i get exactly the same result. So it seems i can call a non static method statically. Why is this? Have i misunderstood this statement?

Then i set about testing another statement



Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.




class Foo {

static public $property = "hello";
public static function aStaticMethod() {
// ...
echo $this->$property;
}


}

Foo::aStaticMethod();


i get
PHP Fatal error: Using $this when not in object context in /Applications/MAMP/htdocs/zend/static2.php on line 8

but i can use


class Foo {

static public $property = "hello";
public static function aStaticMethod() {
// ...
echo self::$property;
}


}

Foo::aStaticMethod();


This works. I'm getting somewhere. But why do i have to use the scope resolution operator :: why can't i use the $this-> in here. I thought the whole point was to allow access to a member without having to create an instantiation. So giving you access from outside as with Foo::aStaticMethod() but why does it matter what you use inside the class.

Im really at a loss here at understanding this whole static concept any help would be greatly appreciated. It seems to me you might as well just use a procedural function instead of a static function.

Fou-Lu
02-17-2012, 12:35 AM
$this != self::.
$this refers to a context object. Static refers to context "self" or "class". Any effect in static reflects on every instance of said object, so it cannot be applied at a level of $this.

PHP is a forgiving language. Issue an error_reporting(E_ALL | E_STRICT);. Strict will indicate you cannot use a static method / variable in a non-static fashion. You can however use a static variable in a non-static method. The third one you have if called in a non-static fashion will complain that the variable is non-static and then issue an error on the read of the member property. This is a great reason why __set should always throw an exception since its the only way to create two variables of the same name (although in different states). $this->property isn't the same as self::$property.

Static isn't designed to work in the context of an object, its designed to work in the context of a class. It can increment a static variable (same as static in a flat procedural function as well) and have it apply to all classes of that type. You can use it to set or change an iterator type you want to apply to any instance of said collection. Its used to create singletons. I use it to merge two objects of a specific type to create a new object of that type. There are a number of great reasons that you may choose to use static.

Static *would* be used in conjunction to make final variables as well. PHP did not choose this route though; instead they introduced the const keyword. Java for example would use public static final int MYNUMBER = 1;.

timgolding
02-17-2012, 11:10 AM
Ok thanks...

I added the E_STRICT level warnings and saw the error message when trying to access a non static method statically. Thanks.

So static method work the same as static variables in the procedural approach. So if i change the static property from hello to goodbye statically any new object or static call to that property will give me the new value 'goodbye'



<?php
error_reporting(E_ALL | E_STRICT);
class Foo {

static public $property = "hello"; // static properies can not be accessed through the object using ->
public static function aStaticMethod() {
// ... A static methods cant use $this-> because they are not in object context.
echo self::$property;
}

public function nonStaticMethod() {
// a non static method cant be called statically.
//Calling non-static methods statically generates an E_STRICT level warning.
}
}

Foo::aStaticMethod(); // outputs hello. called statically with scope resolution operator.
echo "<br />";
Foo::$property = "goobye"; // static property is changed to apply to all classes of that type.
Foo::aStaticMethod(); // outputs goodbye as property is static.

Foo::nonStaticMethod(); // generates an E_STRICT level warning.


echo "<br />";
$foo = new Foo;
$foo->aStaticMethod(); // static property has been changed for all instances of class

?>


I have made this script as a revision script and commented the key points i need to learn. Is there anything else i should know about static? Some examples of where i might use this might also be helpful.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum