php reference passing and returning

伊谢尔伦
Release: 2016-11-22 11:12:45
Original
1106 people have browsed it

Pass by Reference

You can pass a variable to a function by reference so that the function can modify the value of its parameter. The syntax is as follows:

<?php
    function foo(&$var)
    {
        $var++;
    }
    $a=5;
    foo($a);    // $a is 6 here
?>
Copy after login

Note that there are no reference symbols in the function call - only in the function definition. The function definition alone is enough for parameters to be passed correctly by reference. In recent versions of PHP, if you use & in foo(&$a); you will get a warning that "Call-time pass-by-reference" is obsolete.

The following can be passed by reference:

Variables, such as foo($a)

New statements, such as foo(new foobar())

References returned from functions, such as:

<?php
    function &bar()
    {
        $a = 5;
        return $a;
    }
    foo(bar());
?>
Copy after login

Any other expression Neither can be passed by reference, and the result is undefined. For example, the following example of passing by reference is invalid:

<?php
    function bar() // Note the missing &
    {
        $a = 5;
        return $a;
    }
    foo(bar()); // 自 PHP 5.0.5 起导致致命错误
    foo($a = 5) // 表达式,不是变量
    foo(5) // 导致致命错误
?>
Copy after login

These conditions are available in PHP 4.0.4 and later versions.

Reference return

Reference return is used when you want to use a function to find which variable the reference should be bound to. Don't use return references to increase performance, the engine is smart enough to optimize it itself. Only return references if there is a valid technical reason! To return a reference, use this syntax:

<?php
    class foo {
        public $value = 42;
        public function &getValue() {
            return $this->value;
        }
    }
    $obj = new foo;
    $myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
    $obj->value = 2;
    echo $myValue; // prints the new value of $obj->value, i.e. 2.
?>
Copy after login

In this example, the properties of the object returned by the getValue function will be assigned values, not copied, just as if no reference syntax was used.

Note: Unlike parameter passing, the ampersand must be used in both places here - indicating that a reference is returned, not the usual copy. It also points out that $myValue is bound as a reference, not the usual assignment.

Note: If you try to return a reference from a function like this: return ($this->value);, this will not work because you are trying to return the result of an expression rather than a referenced variable. You can only return reference variables from functions - there is no other way. If code attempts to return a dynamic expression or the result of the new operator, an E_NOTICE error will be issued starting with PHP 4.4.0 and PHP 5.1.0.


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!