PHP returns the reference type method, php returns the reference type _ PHP Tutorial

Source: Internet
Author: User
PHP returns the reference type method. php returns the reference type. PHP returns the reference type method. php returns the reference type and returns the reference. when defining and calling the method, lt; php $ colorYellowGreen; functionnbsp; global $ color; return $ color ;} f PHP returns the reference type method, and php returns the reference type

Return reference, which cannot be omitted during definition and call.

This is a confusing concept. if it is not for some special reasons, we recommend that you do not use it.

He is easy to mix because of the changes in the Reference in PHP5, resulting in a large difference in the behavior of PHP4/PHP5.

For example:
The code is as follows:
<? Php

$ Color = 'yellowgreen ';

Function & getRef (){
Global $ color;
Return $ color;
}

Function getCopy (){
Global $ color;
Return $ color;
}

$ ColorRef = & getRef ();
$ ColorRef = 'blue ';

// $ ColorCopy = getCopy ();
// $ ColorCopy = 'black ';


Var_dump ($ color );
Var_dump ($ colorRef );
// Var_dump ($ colorCopy );

Run the above code and you will understand it. use & getRef () to bind $ colorRef to $ color, that is to say, the variables $ colorRef and $ color point to the same value. When one value is changed, the other value also changes.

If $ color is an array, the above code performs the same in any version of PHP4/5.

When $ color is an Object, the problem occurs.

In PHP4, getCopy still returns a copied Object; & getRef () returns a reference.

PHP5 is a little different,
For the latest PHP5.2, the behavior is the same as that for PHP4. getCopy () returns the copy and & getRef () returns the reference.
However, some versions of PHP5 are slightly different here. For example, PHP5.1.6 indicates that both getCopy () and & getRef () are referenced.

Here, getCopy () and & getRef () are two global functions. if they are put into the class as the class's Member functions, it will be another scene ......

PS: returning References does not improve the performance, but sometimes reduces the performance. therefore, do not use this feature to "optimize" the program.

If you have any questions, you can find the answer most of the time in the manual:

Http://cn.php.net/manual/en/language.references.php

Callback returns a reference. During definition and call, lt; php $ color = 'yellowgreen'; function nbsp; global $ color; return $ color;} f...

Related Article

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.