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 static
Is 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::$TEST
Modifyreturn static::$TEST
Expected 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 $className
It should be a variant. It is just a simplification, and nothing special. Because it is obviously equivalent$className = get_class($instance); $obj = new $className;