Class Hw{public static function Hi () {return ' Hello World ';}} Echo Hw::hi ();//OUTPUT:HELLP worldclass hw2{public function hi () {return ' Hello wrold ';}} Echo Hw2::hi ();//OUTPUT:HELLP World
As seen from the example above, the use of static and static properties can be used directly after the:: Method is called directly from outside. But for efficiency and other reasons, it is advisable to use static to limit it.
Static class Internal Call method
Class foo{private static function C () {return ' ABCDE '; } public static function A () {echo self::c (); public static function B () {echo $this->c (); } public Function E () {echo self::c (); }}foo::a ();//output:abcdefoo::b ();//output:fatal error:using $this when not in object context Infoo::e ();//output: Abcef
Methods that use the static keyword to restrict must use self:: Referenced inside this class.
static property
Class Foo{public static $a;p ublic static function A () {self:: $a = ' ABCD ';}} Foo::a ();//output:abcdeecho foo:: $a;
static inheritance and use
Class Foo{public static $a;p ublic static function A () {return ' ABCDE ';}} Class Soo extends foo{public static function A () {echo ' 12345 ';}} Soo::a ();//output:12345
Static inheritance is no different from normal class inheritance methods.
Class Foo{public static $a;p ublic static function A () {return ' ABCDE ';}} Class Soo extends foo{public static function A () {echo parent::a ();}} Soo::a ();//output:12345
It is only used when there is a write difference, it must be the parent:: Method to reference the method of the parents, and can not directly self:: To use the method, otherwise there will be no output:
Class Foo{public static $a;p ublic static function A () {return ' ABCD ';}} class Soo extends foo{public static function AA () {echo self::a ();}} Soo::a ();
The above describes the PHP static methods and properties, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.