Thoughts Caused by reference passing by call_user_func of php

Source: Internet
Author: User
This is a reflection caused by passing reference from call_user_func in php. If you use call_user_func to transfer reference, you can refer to it.

This is a reflection caused by passing reference from call_user_func in php. If you use call_user_func to transfer reference, you can refer to it.

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:


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 we can further trace the root cause. What is the information of this Note? 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 information similar to expected to be a reference, value given. In contrast, if you change the 8th lines of code to & $ x, you will receive 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

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 .}
In general, it means that a warning is printed if the reference is passed to the Hong Kong server and the allow_call_time_pass_reference of php. ini is 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. Hong Kong servers are rented, in the parser stage, php determines whether to pass the reference or value based on 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 ).

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.