Stooney Posted November 8, 2008 Share Posted November 8, 2008 Is it possible to return a reference to a variable? I have a registry class and want a reference to the template object so I don't make a second instance of it. Like this: $template=$registry->get('template'); $template should now be a reference to $registry->data['template']; (data is private to registry). So in the registry it would look like: public function get($key){ if(array_key_exists($key, $this->data)){ return &$this->data[$key]; } } Now of course that return won't work, so is there a way to do this? Also, singletons are out of the question. There are times I may want multiple instances of stuff, but sometimes I just want easier access to something in the registry that I will be using often but need to keep 'globally' accessible. Link to comment https://forums.phpfreaks.com/topic/131972-solved-return-reference/ Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 PHP doesn't support pointer-like things such as that, so I don't think so. >_< Link to comment https://forums.phpfreaks.com/topic/131972-solved-return-reference/#findComment-685740 Share on other sites More sharing options...
Stooney Posted November 8, 2008 Author Share Posted November 8, 2008 That makes me very sad Link to comment https://forums.phpfreaks.com/topic/131972-solved-return-reference/#findComment-685743 Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 Although, in re-reading your question, I feel that I should let you know that objects are automatically passed by reference, so you don't reinstantiate on copy. Your code won't make 2 copies, and you can remove the &. Link to comment https://forums.phpfreaks.com/topic/131972-solved-return-reference/#findComment-685747 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.