PHP Function Reference
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 common function; // delete the reference unset ($ bar); // call $ bar = referenceFunc () as a common function (); $ bar. = "world"; // $ foo does not change echo referenceFunc ();
Function Reference in PHP returns the reference of the function return value to the receiving variable. The value operator is required for function declaration or call &, but it can also be used directly as a common function during the call (in this case, the feature of function reference is not available, and the receiving variable will point to the new memory address ), others include parameter references (value transfer/reference transfer) and object references.
Breakthrough encapsulation
Class Reference {private $ foo; public function & referenceFoo () {return $ this-> foo;} public function getFoo () {echo $ this-> foo ."
";}}$ Obj = new Reference (); // bind a private variable to a common variable $ bar = & $ obj-> referenceFoo (); // external operation private variable $ bar. = "access the private var in outside"; // output 'access the private var in outline' $ obj-> getFoo ();
Of course, function reference is not used to do these things. function reference is mainly used to enable you to bind an additional alias to a certain attribute of the current object for convenient operations.
Note that the "&" operator must be used for definition or call. Otherwise, it is called as a common function or method.