PHP Function Reference

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.