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"; // Не определено свойство my_static print $foo::$my_static . "\n"; // Начиная с PHP 5.3.0 $classname = 'Foo'; print $classname::$my_static . "\n"; // Начиная с PHP 5.3.0 print Bar::$my_static . "\n"; $bar = new Bar(); print $bar->fooStatic() . "\n"; # Пример #2 Пример статического метода /* class Foo { public static function aStaticMethod() { // ... }
<?php class Foo { public static function staticFunction() { return 'static function'; } public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } print Foo::$my_static . "\n"; $foo = new Foo(); print $foo->staticValue() . '<br >'; print $foo->staticFunction() . ' -->with instant' . '<br >'; print Foo::staticFunction() . ' -->without instant';