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 static
is 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;