_php tutorial on reference triggered by PHP's Call_user_func biography

Source: Internet
Author: User
Tags php source code
The question is raised
Netizens Bercmisir in the hospital message, the PHP manual for the Call_user_func function of the document, roughly the following:
http://php.net/manual/en/function.call-user-func.php

One of the parameter has such a sentence:
Note:note the parameters for Call_user_func () is not passed by reference.

A simple translation means that the parameters of this function cannot be passed by reference.

There is one more example:
Copy CodeThe code is as follows:
Error_reporting (E_all);
function increment (& $var)
{
$var + +;
}
$a = 0;
Call_user_func (' increment ', $a);
echo $a. " \ n ";
Call_user_func_array (' Increment ', array (& $a)); You can use this instead before PHP 5.3
echo $a. " \ n ";
?>

The output is:
0
1

and netizens Bercmisir the problem is:
Call_user_func (' increment ', $a), the output is 0, and Call_user_func (' Increment ', & $a), but the output is 1, clearly said cannot rely on the reference to pass.

Root and Trace
And then to further the root of the source, this note is actually http://bugs.php.net/bug.php?id=24931 this bug in the final processing results.
And in Call_user_func (' Increment ', & $a); Although output of 1 results, but generally, there will be a warning message: Deprecated:call-time pass-by-reference has been Deprecated

What is the reason for this?
Let's look at an example:
Copy CodeThe code is as follows:
Error_reporting (E_all);
function increment (& $var)
{
$var + +;
}
$x = 1;
Increment ($x);
echo $x;
?>

The result is 2, and there is no warning message like expected to be a reference, value given, conversely, if you modify the 8th line of code to & $x, you will get a revocation warning. In order to verify, in fact, PHP in the transfer process, the variable will be based on whether the formal parameters need to be referenced or value to decide whether the transmission is a reference or value, do not need to explicitly pass (the opposite is the explicit delivery is about to be abolished).

Continue in depth
http://www.php.net/manual/en/language.references.pass.php
In the PHP manual, we introduce a reference to the pass-through section, where there is a note in the middle that says: there is no need to refer to a function call (that is, the explicit call in the previous section), in 5.3 if the explicit call will come out with a revocation warning.

Analysis Source
Someone says: write in PHP, everything is a reference.
Check the PHP source code, in the./zend/zend_compile.c 1579 line has the function definition Zend_do_pass_param. (php5.2.13)

There is such a sentence of judgment:
if (Original_op = = Zend_send_ref &&! CG (allow_call_time_pass_reference)) {Print repeal warning. }
The meaning is that the reference is passed, and the php.ini allow_call_time_pass_reference is no, print the warning.
Looking at the place where Zend_do_pass_param is used, it can be found in the parser phase, according to the parameter zval the definition of the element in the structure, to pass whether it is var or value or reference. (php5.2.13./zend/zend_language_parser.y/c 451/3593)

Conclusion
References are similar to hard links in Linux, but they are not the same as those in the C language, and PHP will determine whether it is a reference or a value based on the context itself in the parser phase. The call_user_function mentioned in this article will not judge whether a reference or a value is transmitted by itself. So the previous example call_user_function when the value is passed, and the correct result is obtained when the reference is passed (but there is also a revocation warning).

http://www.bkjia.com/PHPjc/322215.html www.bkjia.com true http://www.bkjia.com/PHPjc/322215.html techarticle the problem of the proposed Netizen Bercmisir in the hospital message, the PHP manual for the Call_user_func function of the document, roughly the following: http://php.net/manual/en/ function.call-user-func.php ...

  • 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.