This section describes how to use the static keyword to define static methods and attributes. Static variables can also be used to define static variables and post-static binding. If the declared class property or method is static, you can directly access it without instantiating the class. Static attributes cannot be accessed through an object that has been instantiated by a class (but static methods are acceptable ). To be compatible with PHP4, if no access control is specified, attributes and methods are considered public. This section describes how to use the static keyword to define static methods and attributes. Static variables can also be used to define static variables and post-static binding.
If the declared class property or method is static, you can directly access it without instantiating the class. Static attributes cannot be accessed through an object that has been instantiated by a class (but static methods are acceptable ).
To be compatible with PHP 4, if no access control is specified, attributes and methods are considered public.
Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods.
Static attributes cannot be accessed by objects through the-> operator.
Calling a non-static method in static mode will result in an E_STRICT error.
Just like all other PHP static variables, static attributes can only be initialized as text or constants and cannot use expressions. Therefore, static attributes can be initialized as integers or arrays, but they cannot be initialized as another variable or function return value or point to an object.
Example #1 static property Example
class Foo{ public static $my_static = 'foo'; public function staticValue(){ return self::$my_static; }}class Bar extends Foo{ public function fooStatic(){ return parent::$my_static; }}print Foo::$my_static.'
';$foo = new Foo();print $foo->staticValue().'
';print $foo->my_static.'
';print $foo::$my_static.'
';$classname = 'Foo';print $classname::$my_static;print Bar::$my_static.'
';$bar = new Bar();print $bar->fooStatic().'
';