Run
Run

Reputation: 57176

Registry pattern - the difference with reference and without

Following this guide, but it is always difficult for me to understand references in PHP. What is the purpose of using reference in the params below?

public function register($name, &$object)
{
    $this->registries[$name] =& $object;
}
public function &getRegistry($name)
{
    return $this->registries[$name];
}

Without the references:

public function register($name, $object)
{
    $this->registries[$name] = $object;
}

public function getRegistry($name)
{
    return $this->registries[$name];
}

It works fine too without the the references, so what is the advantages having them?

Upvotes: 0

Views: 72

Answers (1)

deceze
deceze

Reputation: 522085

Objects needed to be explicitly passed by reference back in the PHP 4 dark ages. Since PHP 5.0 objects essentially are a reference and it doesn't make any difference whether you pass the object reference by reference or not. Every guide on the matter will tell you to omit passing objects by reference.

Upvotes: 2

Related Questions