Comparison of usage differences between new self () and new Static () in object-oriented PHP

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.