PHP return Reference (method name plus &) and local static variable (static)

Source: Internet
Author: User

Read the manual first
==========
To return a reference from a function, you must use the reference operator & when the function declares and assigns a return value to a variable :

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

<?php function &returns_reference () {$someref = 0;  return $someref;  } $newref = &returns_reference ();//equivalent to $newref = & $someref; ?>


For more information about references, see the explanations of the references.



Looking at a lot of open source code like to use the single-case registration mode

CLASS&NBSP;A{}&NBSP;&NBSP;CLASS&NBSP;B{}&NBSP;&NBSP;FUNCTION&NBSP;&AMP;&NBSP;AA ($name)   {       static  $class  = array ();  //local static variable   does not disappear with the execution of the method   But to stick to the end of the entire source program to end the life cycle       if (! $class [$name])         //so   The declaration/initialization statement here   only works on first declaration       {                          //After you call this method,  static variables are no longer reinitialized value           $class [$ name] = new  $name ();      }       return  $class [$name];  }   $a  = & aa (' a ');   $b  = &NBSP;&AMP;&NBSP;AA (' B ');     $a 2 = & aa (' a ');   $b 2 = &NBSP;&AMP;&NBSP;AA (' B ');     echo  $a  ===  $a 2 ?  ' $a and $a2 are the same instantiated objects <br/> '  :  ';   echo  $b  ===  $b 2 ?  ' $b and $B2 are the same instantiated objects '  :  ';


PHP return Reference (method name plus &) and local static variable (static)

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.