class t {
public function tt()
{
echo 1;
}
}
t::tt();
See?The non-static function can also be called at class level.So what's different if I add a static
keyword before public
?
class t {
public function tt()
{
echo 1;
}
}
t::tt();
See?The non-static function can also be called at class level.So what's different if I add a static
keyword before public
?
The Static Keyword
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.
Just because you can call non-static methods statically doesn't mean you should. It's bad form.
Except that, if you try to use $this
in your method, like this :
class t {
protected $a = 10;
public function tt() {
echo $this->a;
echo 1;
}
}
t::tt();
You'll get a Fatal Error when calling the non-static method statically :
Fatal error: Using $this when not in object context in ...\temp.php on line 11
i.e. your example is a bit too simple, and doesn't really correspond to a real-case ;-)
Also note that your example should get you a strict warning (quoting) :
Calling non-static methods statically generates an
E_STRICT
level warning.
And it actually does (At least, with PHP 5.3) :
Strict Standards: Non-static method t::tt() should not be called statically in ...\temp.php on line 12
1
So : not that good ;-)
Still, statically calling a non-static method doesnt't look like any kind of good practice (which is probably why it raises a Strict warning), as static methods don't have the same meaning than non-static ones : static methods do not reference any object, while non-static methods work on the instance of the class there're called on.
Once again : even if PHP allows you to do something (Maybe for historical reasons -- like compatibility with old versions), it doesn't mean you should do it !
In general a static method is also called class method while a non-static method is also called object method or instance method.
The difference between a class method and an object method is that class methods can only access class properties (static properties) while object methods are used to access object properties (properties of the very same class instance).
Static methods and properties are used to share common data over or for all instances of that specific class.
You could, for example, use a static property to keep track of the number of instances:
class A {
private static $counter = 0;
public function __construct() {
self::counter = self::counter + 1;
}
public function __destruct() {
self::counter = self::counter - 1;
}
public static function printCounter() {
echo "There are currently ".self::counter." instances of ".__CLASS__;
}
}
$a1 = new A();
$a2 = new A();
A::printCounter();
unset($a2);
A::printCounter();
Note that the static property counter is private so it can only be accessed by the class itself and instances of that class but not from outside.