Examples:
Static method example
1 2 3 4 5 6 7 8 9 10 11 | <?php class Foo { public static function aStaticMethod() { // ... } } Foo::aStaticMethod(); $classname = 'Foo' ; $classname ::aStaticMethod(); // As of PHP 5.3.0 ?> |
Static property example
Static properties cannot be accessed through the object using the arrow operator ->.
Like any other PHP static variable, static properties may only be initialized using a literal or constant before PHP 5.6; expressions are not allowed. In PHP 5.6 and later, the same rules apply as const expressions: some limited expressions are possible, provided they can be evaluated at compile time.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self, parent and static).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php 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 . "\n" ; $foo = new Foo(); print $foo ->staticValue() . "\n" ; print $foo ->my_static . "\n" ; // Undefined "Property" my_static print $foo :: $my_static . "\n" ; $classname = 'Foo' ; print $classname :: $my_static . "\n" ; // As of PHP 5.3.0 print Bar:: $my_static . "\n" ; $bar = new Bar(); print $bar ->fooStatic() . "\n" ; ?> |
Please login to continue.