How do you understand this example in the PHP manual?

Source: Internet
Author: User
http://php.net/manual/zh/language.oop5.basic.php
PHP 5.3.0 introduces two new ways to create an instance of an object:
Example creating a new object #5


  
   

What do you mean, return new static in line six?
$obj 2 of line 14th = new $obj 1; Shouldn't the class name be followed by new? What is the case here?

Reply content:

http://php.net/manual/zh/language.oop5.basic.php
PHP 5.3.0 introduces two new ways to create an instance of an object:
Example creating a new object #5


  
   

What do you mean, return new static in line six?
$obj 2 of line 14th = new $obj 1; Shouldn't the class name be followed by new? What is the case here?

Http://www.5dphp.com/art_117.html

return new staticis to create a new current class object and return the meaning, please refer to: http://stackoverflow.com/questions/16977369/php-new-staticvariable

After the object is also PHP 5.3 new features, the equivalent of the new Test() bar, you can see this test: Http://3v4l.org/XMKAI

Declaring that I am a regular developer, I am more concerned with how to use the code instead of asking why.
1 static is a new keyword that can be understood as (or translated into a document's content) for late binding. For example

class A {    public static $TEST = 1;    public function get() {        return self::$TEST;    }}class B {    public static $TEST = 2;}

A B object instance executes a GET method that gets the test value defined in the A object declaration, not B itself, but return self::$TEST modifies return static::$TEST it to get the expected result. The most obvious benefit is that the inheritance of one-piece objects becomes easier.

2 As for the new $obj, should it be a magic notation? The way it used to be $className = 'stdClass'; $obj = new $className constructed, it should be a variant, just a simplification, nothing special. Because it's obviously equivalent to$className = get_class($instance); $obj = new $className;

  • Related Article

    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.