Thoughts caused by reference passing by call_user_func of php _ PHP Tutorial

Source: Internet
Author: User
The thought caused by passing reference from call_user_func of php. The problem is raised by the netizen bercmisir in the hospital message, for the php manual call_user_func function document, roughly as follows: php. netmanualenfunction. call-user-func.php where Question proposal
The netizen bercmisir left a message in the hospital. the document about call_user_func function in the php Manual is roughly as follows:
Http://php.net/manual/en/function.call-user-func.php

Parameter has the following sentence:
Note: Note that the parameters for call_user_func () are not passed by reference.

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

Another example is as follows:

The code is as follows:


Error_reporting (E_ALL );
Function increment (& $ var)
{
$ Var ++;
}
$ A = 0;
Call_user_func ('credentials', $ );
Echo $ a. "\ n ";
Call_user_func_array ('secret', array (& $ a); // You can use this instead before PHP 5.3
Echo $ a. "\ n ";
?>


The output is:
0
1

The problem with bercmisir is:
Call_user_func ('credentials', $ a); the output is 0, while call_user_func ('credentials', & $ a); the output is 1, which clearly cannot be passed by reference.

Root tracing
Then further tracing, the Note information is actually http://bugs.php.net/bug.php? Id = 24931 the final result of the bug.
And in call_user_func ('credentials', & $ a); although the result of 1 is output, there is usually a warning message: Deprecated: call-time pass-by-reference has been deprecated.

Why?
Let's take a look at an example:

The 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 similar to expected to be a reference, value given. On the contrary, if you change the 8th line of code to & $ x, you will get an abolition warning. So as to be verified, in fact, during the transfer process of PHP, the variables will decide whether to transmit the reference or value based on whether the reference or value is required by the form parameter from the row, explicit transfer is not required (On the contrary, explicit transfer is about to be abolished ).

Continue
Http://www.php.net/manual/en/language.references.pass.php
In the php manual, we will introduce the reference transfer section. there is a Note in the middle of the section: when calling a function, you do not need to pass the reference (that is, the explicit call mentioned in the previous section). In 5.3, if the explicit call is performed, an abolition warning is returned.

Source Code Analysis
Some people say that everything is a reference for writing in php.
Check the php source code. in line 2 of./Zend/zend_compile.c, there is a function definition zend_do_pass_param. (Php5.2.13)

Here is a judgment:
If (original_op = ZEND_SEND_REF &&! CG (allow_call_time_pass_reference) {print the abolition warning .}
This means that a warning is printed if reference is passed and allow_call_time_pass_reference of php. ini is set to No.
Looking at zend_do_pass_param, we can find that in the parser stage, we can pass whether it is var, value, or reference based on the definition of the element in the ZVAL struct parameter. (Php5.2.13./Zend/zend_assistage_parser.y/c 451/3593)

Conclusion
References are similar to hard links in linux, but they are different from pointers in C. in the parser stage, php will determine whether to pass the reference or the value according to the context. The call_user_function mentioned in this article does not determine whether the parameter is referenced or a value. Therefore, the preceding example call_user_function does not work when passing values, but produces the correct result when passing references (but there is also an abolition warning ).

The netizen bercmisir leaves a message in the hospital, for the php manual call_user_func function documentation, roughly as follows: http://php.net/manual/en/function.call-user-func.php of which...

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.