Solving the problem of constructor parameter of class in PHP

Source: Internet
Author: User
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.

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.