deviantgeek6966 Posted December 24, 2006 Share Posted December 24, 2006 alright, i know how to reference an object or variable. i know what referencing is, but ive been trying to write an object that actually needs to reference itself. so nobody jumps to conclusions i cant just use $this instead of referencing to it. i need to actually reference the object. the problem is that when i save the reference inside my object it shows up as a reference when i do a var_dump but its actually only a copy of the object. ill show my actual code if i need to if it helps to understand but i figure ill write this little bit of code to show my problem more easier. this will produce my same problem.[code]<?phpclass my_class{ var $reference; var $some_var; function my_class() { //save a reference to this object $this->save_reference(&$this); } function save_reference(&$reference) { $this->reference =& $reference; }}//create my object$obj = new my_class();//now that my_class() referenced $obj to $obj->reference (any changes to $obj should reflect in $obj->refence). so now i will change a variable in $obj$obj->some_var = 'blah';//now if things worked right, $obj->reference should point to $obj and should see the change i made to $obj->some_vardie($obj->reference->some_var);?>[/code]the output is blank, which can only mean $obj->reference was never actually referenced, it was copied instead. now i dont know if im misunderstanding how referencing works or if im completely missing something. ive looked all over and i have no idea how to fix this or understand whats going on. any help would be great. thanks Link to comment https://forums.phpfreaks.com/topic/31746-solved-php-referencing-a-class/ Share on other sites More sharing options...
deviantgeek6966 Posted December 24, 2006 Author Share Posted December 24, 2006 whelp nm, i finally figured out what was going on. it turns out that when i actualy create my object i need to reference it at the same time or else it wont work. this is what i did to actually make it work:[code]<?phpclass my_class{ var $reference; var $some_var; function my_class() { $this->reference = &$this; } function save_reference(&$reference) { $this->reference =& $reference; }}//return a reference to my object created, this is where i had my problem$obj =& new my_class();$obj->some_var = 'blah';die($obj->reference->some_var);?>[/code] Link to comment https://forums.phpfreaks.com/topic/31746-solved-php-referencing-a-class/#findComment-147220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.