As we all know, the purpose of OOP is to write code once, copy and copy, inherit and inherit the reasonable use of every work, but also convenient code management. Which link is wrong to find which link. But sometimes we write a class that eventually finds it only used once, and does not generate multiple instances. This time again to new, not only affect the efficiency, the code also appears not concise.
So very emotional PHP, provides us with a convenient and efficient way to static with it all is no longer a problem.
Let me tell you about the use of static methods and characteristics.
Hello World:
Class hw{public static function hi () { return ' Hello world!!! '; }} Echo Hw::hi ();
Class hw{public function hi () { return ' Hello world!!! '; }} Echo Hw::hi ();
As you can see from the example above, using static and not using the static property can be used directly: The method is called directly from the outside, but it is recommended to use static for efficiency and other reasons.
Static class Internal Call method
Class foo{private static function C () { return ' ABCD ';} public static function A () { echo self::c ();}} Foo::a ();
The use of the static keyword to restrict a method is that you must use self:: Within this class, the above example is very clear.
static property
Class Foo{public static $a;p ublic static function A () {self :: $a = ' ABCD ';}} Foo::a (); Echo foo:: $a;
We can also use the static keyword to limit the value of a variable at this time when the variable does not keep the last negative.
static inheritance and use
Class Foo{public static $a;p ublic static function A () { return ' ABCD ';}} Class Oo extends foo{public static function A () { echo ' 1234 ';}} Oo::a ();
Static inheritance is the same as the normal class inheritance method, and it is not much different.
Class Foo{public static $a;p ublic static function A () { return ' ABCD ';}} Class Oo extends foo{public static function aa () { echo parent::a ();}} Oo::aa ();
It is only used when there is a write difference, must be the parent:: Method to refer to the parent class method, and does not directly self:: To use the parent class method, as the following example, will not display any value:
Class Foo{public static $a;p ublic static function A () { return ' ABCD ';}} class Oo extends foo{public static funct Ion AA () { echo self::a ();}} Oo::a ();
One more simple example.
?
http://www.bkjia.com/PHPjc/752510.html www.bkjia.com true http://www.bkjia.com/PHPjc/752510.html techarticle As we all know, the purpose of OOP is to write code once, copy and copy, inherit and inherit the reasonable use of every work, but also convenient code management. Which ring ...