Cause of the problem
Two days ago, someone in the group said a question about new and StdClass, as follows:
<?php$a = new StdClass; $b = new $a; Var_dump ($a, $b);
This code works correctly, and $a and $b are two different empty objects. Even if you add a property and assign a value to the $a before the new $a, $b is always an empty object.
So the question is: why is it that the empty object can be followed by new, and what's special about StdClass?
Actual performance
In fact, a little verification will be able to know, in fact, this and StdClass does not have any relationship, is entirely new behavior decided, such as in Psysh to do a simple test:
>>> $a = new reflection;=> Reflection {#174}>>> $b = new $a;=> Reflection {#177}
Here I am new. An instance of the Reflection class, with no distinction between the performance of StdClass. Of course, you can also customize a class:
>>> class Test {public $foo = 1;} = = Null>>> $a = new test=> Test {#178 +foo:1, }>>> $a->foo = 2;=> 2>>> $b = new $a;=> Test {#180 +foo:1, }
As we can see clearly in this example, changing the properties of a $a has no effect on the $b (here you can also think of a keyword in PHP: clone).
Now that you know the performance, you can also get the conclusion that a new object is created by a class object to be the same class as the original object.
Reason
So what's the implementation of PHP that's causing this kind of performance? or from the source to resolve the problem.
In fact, from the source code, we can straight zend_vm_def.h to find the answer, in about zend_fetch_class This opcode explanation, we can see the following content:
Zend_vm_handler (109, Zend_fetch_class, any, const| tmpvar| unused| CV) { ... if (Op2_type = = is_const) { ... } else if (z_type_p (class_name) = = Is_object) { z_ce_p (Ex_var (opline-> Result.var)) = Z_objce_p (class_name); } ... ...}
Remove some of the interference context, the above content is very clear to show an explanation: If the class_name is an object, then through the Z_objce_p macro to find its class. So the above performance is easy to explain.
This is a very simple question in itself, not to be complicated. If you want to know the implementation of the specific new, you can go to the zend_compile.c file to see the implementation of the zend_compile_new.