PHP return reference (method name prefix &) and local static variable (static)

Source: Internet
Author: User
PHP return reference (method name prefix &) and local static variable (static) read manual first
============
To return a reference from a function, you must use the reference operator when declaring the function and assigning the return value to a variable &:

Example 17-13. a reference is returned by the function.

 


For more information about references, see references.



Let's take a look at a lot of examples of registration modes that open-source code prefers.

Class a {} class B {} function & aa ($ name) {static $ class = array (); // The local static variable does not disappear with the execution of the method, but ends the lifecycle until the end of the entire source program. if (! $ Class [$ name]) // so the declaration/initialization statement here only takes effect at the first declaration {// and then calls this method, static variables no longer reinitialize 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 objects.
': ''; Echo $ B ===$ b2? '$ B and $ b2 are the same instantiated objects ':'';

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.