Read the manual first
==========
To return a reference from a function, you must use the reference operator & when the function declares and assigns a return value to a variable :
Example 17-13. A reference is returned by the function
<?php function &returns_reference () {$someref = 0; return $someref; } $newref = &returns_reference ();//equivalent to $newref = & $someref; ?>
For more information about references, see the explanations of the references.
Looking at a lot of open source code like to use the single-case registration mode
CLASS&NBSP;A{}&NBSP;&NBSP;CLASS&NBSP;B{}&NBSP;&NBSP;FUNCTION&NBSP;&&NBSP;AA ($name) { static $class = array (); //local static variable does not disappear with the execution of the method But to stick to the end of the entire source program to end the life cycle if (! $class [$name]) //so The declaration/initialization statement here only works on first declaration { //After you call this method, static variables are no longer reinitialized value $class [$ name] = new $name (); } return $class [$name]; } $a = & aa (' a '); $b = &NBSP;&&NBSP;AA (' B '); $a 2 = & aa (' a '); $b 2 = &NBSP;&&NBSP;AA (' B '); echo $a === $a 2 ? ' $a and $a2 are the same instantiated objects <br/> ' : '; echo $b === $b 2 ? ' $b and $B2 are the same instantiated objects ' : ';
PHP return Reference (method name plus &) and local static variable (static)