Home >Backend Development >PHP Tutorial >Understand PHP's return references and local static variables, php local static variables_PHP tutorial

Understand PHP's return references and local static variables, php local static variables_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:51:51832browse

Understand PHP's return references and local static variables, php local static variables

Read the manual first
==========
To return a reference from a function, you must use the reference operator & both when declaring the function and assigning the return value to a variable:
Copy code The code is as follows:
function &returns_reference()
{
$someref = 0;
Return $someref;
}

$newref = &returns_reference();//Equivalent to $newref = &$someref;
?>

For more information about citations, check out the explanation of citations.

Let’s take a look at a single instance registration model that many open source codes like to use
Copy code The code is as follows:
class a{}
class b{}
function & aa($name)
{
static $class = array(); //Local static variables do not disappear when the execution of the method ends, but persist until the end of the entire source program to end the life cycle
If(!$class[$name]) //So the declaration/initialization statement here only works when it is first declared
                                                                                                                        // When this method is called later, the static variable will no longer re-initialize the value
          $class[$name] = new $name();
}  
Return $class[$name];
}
$a = & aa('a');
$b = & aa('b');

$a2 = & aa('a');
$b2 = & aa('b');

echo $a === $a2 ? '$a and $a2 are the same instantiated object
' : '';
echo $b === $b2 ? '$b and $b2 are the same instantiated object' : '';

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1011953.htmlTechArticleUnderstand PHP’s return references and local static variables. PHP local static variables read the manual first ======= === Returning a reference from a function must be done when the function is declared and the return value is assigned to a variable...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn