arianhojat Posted February 13, 2007 Share Posted February 13, 2007 had question about References in the php manual.... "When assigning an already created instance of an object to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A new instance of an already created object can be made by cloning it." class SimpleClass { // member declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; } } $instance = new SimpleClass(); $assigned = $instance; $reference =& $instance; $instance->var = '$assigned will have this value'; $instance = null; // $instance and $reference become null var_dump($instance); var_dump($reference); var_dump($assigned); //prints out Now the manual seems to suggest u have to manually clone an object if you want to copy it to another variable by using clone() in php5, and just assigning a variable to an object, that it will make it a reference and not a copy. Is that really true? cause it seems to suggest u have to manually have to add &= for reference to work (unless use 'new' operator which automatically adds a &=). $assigned seems to have cloned its own copy of $instance without using clone(). Link to comment https://forums.phpfreaks.com/topic/38356-reference-question/ Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 They changed this between 4 & 5, which version are you using, and which are you reading the docs for? Link to comment https://forums.phpfreaks.com/topic/38356-reference-question/#findComment-183854 Share on other sites More sharing options...
arianhojat Posted February 13, 2007 Author Share Posted February 13, 2007 version 5 for both Link to comment https://forums.phpfreaks.com/topic/38356-reference-question/#findComment-183856 Share on other sites More sharing options...
arianhojat Posted February 14, 2007 Author Share Posted February 14, 2007 bump Link to comment https://forums.phpfreaks.com/topic/38356-reference-question/#findComment-184570 Share on other sites More sharing options...
arianhojat Posted February 14, 2007 Author Share Posted February 14, 2007 uh oh, the dreaded "double bump". dont want to open a new topic, although i imagine bumps are frowned upon so maybe i will do that if no answer. Link to comment https://forums.phpfreaks.com/topic/38356-reference-question/#findComment-184730 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.