Jump to content

[SOLVED] Return Reference


Stooney

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.