The detailed code is as follows:
<?phpfinal class Registry { private $data = Array (); Public function Get ($key) { return $this->data[$key]; } Public function set ($key, $value) { $this->data[$key] = $value; }} Abstract class Controller { protected $registry; Public function construct ($registry) { $this->registry = $registry; }} Class Controllercommonhome extends Controller {public function test () { $this->registry->set (' Load '), ' Load ');} } $registry = new Registry (), $con = new Controllercommonhome ($registry), $con->test (), Echo $registry->get (' Load '); >
How is the parameter $registry passed in when the $con object is initialized? Pass the value or pass the address? If it is a value, then the $con object executes the function test () without affecting the original object $registry, but the fact is that the value of registry is affected.
But if I change the $registry to a simple variable, such as $registry = 123, it also assigns a value to the $registry in the $con object, which does not affect the original variable, and it is still 123.
Solving
OK, Clone () constructor. Object is can be cloned, why cloning, just do not want to change its earlier state
object is not a general variable, please see the following section of Google search
Object delivery
PHP5 using Zend Engine II, objects are stored in a separate structure of the object store, Instead of storing it in zval like any other generic variable (in PHP4, the object is stored in zval like a generic variable). Only pointers to objects, not content (value), are stored in zval. When we copy an object or pass an object as a parameter to a function, we do not need to copy the data. Just keep the same object pointer and be notified by another zval now that this particular object is pointing to the object Store. Since the object itself is in object Store, any changes we make to it will affect all zval structures that hold the pointer to the object----in the program that any change to the target object will affect the source object: This makes the PHP object look like always by reference (reference ), so the object in PHP is passed by "reference" by default, and you no longer need to declare it using & As in PHP4.
I've never thought about it before, and I've been looking for information when I see your problem.