通过引用传递 - 它是如何工作的以及为什么使用它?

Take the following code from CodeIgniter's show_error function:

$_error =& load_class('Exceptions', 'core');

The documentation for the load_class function says it acts as a singleton. The function basically takes the given parameters and searches for a class in the appropriate path. It then includes the file if it exists. The function is declared as:

function &load_class(...)

Why does it have the & and what is its purpose? Is $_error declared as such as a result of defining the function like that?

The php documentation seems to explain why you have to uses =& even though the function is marked to return a refrence function &load_class

Returning References

Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so. To return references, use this syntax:

 <?php class foo {
     public $value = 42;

     public function &getValue() {
         return $this->value;
     } 
  }



$obj = new foo; 
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2; 
echo $myValue;
// prints the new value of $obj->value, i.e. 2. ?> 

In this example,

the property of the object returned by the getValue function would be set, not the copy, as it would be without using reference syntax. Note: Unlike parameter passing, here you have to use & in both places - to indicate that you want to return by reference, not a copy, and to indicate that reference binding, rather than usual assignment, should be done for $myValue.

If you are asking what references in general are the documentation explains.

I don't see any point of declaring and using load_class like that. From the source code of load_class(), we can see that it caches loaded objects in an array with the class name as the key. If it is not in the cache, it loads an object given a name, and then stores that object reference into the array. In both cases, it returns the element of the array (by reference).

Returning by reference allows the caller to have a reference to the element of the array. The only things that this allows us to do are:

  1. See later changes to that array element (i.e. the value associated with that key) from the outside reference we have. But this is not applicable, since the load_class function never changes the value associated with a key after it sets it.
  2. Have external code be able to change the element in the array, without the load_class function knowing about it. But this would be a highly dubious practice, to mess with the cache from the outside, and I highly doubt this is something the authors wanted.

So there is no legitimate reason to return by reference. My guess is that it is a leftover from PHP 4, when objects were values, and so assigning or returning an "object value" would copy it. In PHP 5, objects are not values; you can only manipulate them through object references, and assigning or returning an object reference by value never copies the object it points to.