Php constructor parameters

Source: Internet
Author: User
The detailed code for the class constructor parameters in php is as follows: & lt ;? PhpfinalclassRegistry {& nbsp; private $ dataarray (); & nbsp; publicfunctionget ($ key) {& nbsp; return $ this-& gt; data [$ key]; & nbsp ;} & php constructor parameters
The code is as follows:
Final 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 ');
?>
In $ con object initialization, how does the parameter $ registry pass in? Value transfer or address transfer? If the value is passed, the original object $ registry is not affected when the $ con object executes the test () function, but the registry value is affected.
However, if I change $ registry to a simple variable, for example, $ registry = 123, $ registry is assigned a value to $ registry in the $ con object, but this does not affect the original variable, still 123.
Solution

------ Solution --------------------
Okay, _ clone () constructor. The object can be cloned. for the sake of cloning, you just don't want to change its previous state.
The object is not a common variable. please refer to the following Google-found section.

Object transfer
PHP5 uses Zend Engine II, and objects are stored in an independent structure Object Store, unlike other common variables stored in Zval (objects in PHP4 are stored in Zval like General variables ). In Zval, only the object pointer is stored, not the content (value ). When we copy an object or pass an object as a parameter to a function, we do not need to copy data. Only keep the same Object pointer and the other zval notifies the Object Store to which the specified Object points. Because the Object is located in the Object Store, any changes we make to it will affect all the zval structures that hold the object pointer-any changes to the target object in the program will affect the source object .. This makes PHP Objects look like they are always passed through reference, so objects in PHP are passed through reference by default, you no longer need to use & As in PHP4.

I have never thought about this problem before. I went to find the information only when I saw 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.