New Static () is an additional feature introduced in the PHP5.3 version, whether it is new static () or new self (), which will be used to update the object. The following article mainly introduces you to the PHP object-oriented new self () and new Static () of the difference, the need for friends can refer to, let's take a look at it.
Objective
This article mainly introduces the difference between the new self () and the New Static () in the object of PHP, what is the difference between the two methods of the new object, and whether the new one is the same class instance or a different class instance? The following words do not say much, come together to see the detailed introduction.
The difference is as follows:
First, the conclusion is that in PHP, self points to the class that defines the currently called method, and static points to the class that invokes the current static method.
Next, an example is given to prove the result.
Class A {public static $_a = ' Class a ', public static function Echoproperty () {echo self::$_a. Php_eol; }}class B extends A {public static $_a = ' class B ';} $obj = new B (); B::echoproperty ();//Output Class A
This is because using self: or __class__ a static reference to the current class, depending on the class that defines the called method, Echoproperty the method above Class A to make the modification:
Class A {public static $_a = ' Class a ', public static function Echoproperty () {echo static::$_a. Php_eol; }}//call B::echoproperty again to output ' CLASS B '
To avoid overriding the static properties of the parent class with the subclasses seen in the first example above, the inherited method still accesses the static properties of the parent class, PHP5.3 adds a new syntax: late static binding (late static binding), replacing the Self keyword with the static keyword , which causes static to point to the same class that is returned with Get_called_class (), the class that is currently calling the static method, which is equally valid for access to the static method.
The following example better illustrates the difference between the new self () and the New Static () (which uses a late static binding of PHP to the current class of the Calling method)
Class A {public static function get_self () {return to New self (),} public static function Get_static () {return NE W Static (); }}class B extends A {}echo get_class (b::get_self ()); Aecho Get_class (B::get_static ()); Becho Get_class (A::get_self ()); Aecho Get_class (A::get_static ()); A