function &referencefunc () { static $foo = '; return $foo; } Pass a reference to the static variable $foo to $name$bar = &referencefunc (); $bar. = "Hello";//Call Echo Referencefunc () as a normal function;//delete reference unset ($bar) ;//As a normal function call $bar = Referencefunc (); $bar. = "World";//$foo does not change echo referencefunc ();
PHP's function reference is to return a reference to the function return value to the receive variable, the function declaration or call need to use the value operator &, but the call can also be used as a normal function directly (there is no function to reference the attribute, the receiving variable will point to the memory of the new address), There are also parameter references (value passing/reference passing), object reference
Breakout Class Package
Class Reference { private $foo; Public Function &referencefoo () { return $this->foo; } Public Function Getfoo () { echo $this->foo. "
"; }} $obj = new Reference ();//Bind the private variable on a common variable $bar $bar = & $obj->referencefoo ();//external operation private variable $bar. = "Access the private var in outside ";//output ' access the private var in outside ' $obj->getfoo ();
Of course function references are not used to do these things, and the main use of function references is to allow you to bind an additional alias to a property of the current object for easy operation
Note that the "&" operator is used when defining or calling, otherwise it is called as a normal function or method