How does one understand this example in the PHP manual?

Source: Internet
Author: User
Php. netmanualzhlanguage. oop5.basic. phpPHP5.3.0 introduces two new methods to create an object instance: Example #5 create a returnnewstatic object in line 6 of {code? $ Obj2new $ obj... http://php.net/manual/zh/language.oop5.basic.php for row 14th
PHP 5.3.0 introduces two new methods to create an object instance:
Example #5 create an object


  

What does return new static in the sixth row mean?
$ Obj2 = new $ obj1; shouldn't new be followed by the class name? How is an instance written here?

Reply content:

Http://php.net/manual/zh/language.oop5.basic.php
PHP 5.3.0 introduces two new methods to create an object instance:
Example #5 create an object


  

What does return new static in the sixth row mean?
$ Obj2 = new $ obj1; shouldn't new be followed by the class name? How is an instance written here?

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

return new staticIs to create a new current Class Object and return the meaning, see: http://stackoverflow.com/questions/16977369/php-new-staticvariable

The receiving object is also a new function of PHP 5.3, which is equivalentnew Test()Now, you can see this test: http://3v4l.org/XMKAI

Declare that I am a common developer. I pay more attention to how to use code instead of asking why.
1 static is a New Keyword, which can be understood as (or translated as) for late binding. For example

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

When the get method is executed on object B, the TEST value defined in object A is obtained, instead of the TEST value defined in object B.return self::$TESTModifyreturn static::$TESTExpected results are displayed. The most obvious advantage is that the inheritance of a single object becomes easier.

2 As for new $ obj, what is magic? Previously Available$className = 'stdClass'; $obj = new $classNameIt should be a variant. It is just a simplification, and nothing special. Because it is obviously equivalent$className = get_class($instance); $obj = new $className;

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.