What is Call Time Pass Reference? What does it do?开发者_StackOverflow中文版
The problem of not letting me use call time reference is that I can't change the declaration of php's built-in functions, I've used a lot of call time references on built in functions, I know exactly what these functions do and I know that no mischiefs will happen, I need to use the feature and I can't rewrite these function, besides it not always I need it to be passed by reference, I need it on specific situations, for example:
$rows[] = array_map('array_shift', &$data);
instead of doing a foreach statement, I save time and coding lines by doing an array_map
with and array shift as callback, the problem is (I don't know if its a bug) that array_shift
doesn't get my array by reference (I guess its because the reference thing works only for 1 level if you get a referenced value and throws it on another function that doesn't get by reference it loses the reference, I wish php had real pointers and not this reference thing) thus I need to pass the array by reference at call time.
Another example
new ArrayObject(&$_POST, ArrayObject::ARRAY_AS_PROPS)
I want my $_POST
as an array object, but if I unset something I want that to happen on the real $_POST
, and so I could do with $_SESSION
and $_GET
, I know what I'm doing in this case and if it's a bad practice I cant see the problem with it, instead of doing that I had to create my own object that got the array by reference, I lost some time with that....
The point is, any language out there allows you to pass pointers to functions that wasn't designed to receive pointers in first place, maybe that's a bad practice but it's up to me to decide it, isn't it?
This feature shouldn't be deprecated.
I assume you mean Call Time Pass By Reference.
In PHP, the &
operator will get the reference of the variable (There is lots of info on this on stackoverflow)
Call time pass by reference is where you pass a reference as an argument to a function that doesn't take a reference
function foo($myParam){
}
//Call Time Pass By Reference
foo(&$myArg);
This will give a warning in PHP 5.3 as it as been deprecated and it will be passed by value. The correct way to do it is to alter the function foo to take a reference
function foo(&$myParam){
}
foo($myArg); //myArg is passed by reference
The ability to preform call time pass by reference is controlled by the ini setting
allow_call_time_pass_reference
In more recent releases this is off by default.
The reason why it is bad is that the function (and reader) doesn't know which parameters are references and which aren't. It makes the code a lot harder to follow.
The PHP manual explains it:
Nowadays, references are declared with the function declaration like this:
function foo (&$bar) {
// changing $bar changes the original variable
}
However, the syntax also allows for passing a variable by reference, when you call the function:
foo(&$bar);
and that is deprecated, since it is considered bad style and error-prone. (If a function doesn't know, if it acts on a copy or the original variable, you can think of any mischief happening.)
The latter one is called call time pass by reference, since the reference is passed at function call time. (Hm... The first rule of the tautology club is the first rule of the tautology club.)
In PHP when you pass the value to a function it is copied for function to use it so outside of the function the value is intact. This is called passing by value. If you want to modify value inside the function you would make a pass by reference (pass a pointer to a value, not the value itself). I PHP this is done by using &
character.
$a = 1;
function b($a) {
$a + 1;
echo $a;
}
echo $a; // print 1
echo b($a); // print 2
echo $a; // $a is still 1
echo b(& $a); // print 2
echo $a; // $a is now 2
If you want to pass a reference you have two ways of doing that in PHP and one of it (call-time) is deprecated.
Call-time pass by reference:
b(& $a);
What you should do instead is change your function declaration and add &
there.
// declare reference parameter
function b(& $a) {
...
}
b($a); // pass by reference
In PHP 5.3.x we were getting warning that call-time pass by reference was deprecated.
But note that as of PHP 5.4.0 using a call-time pass by reference is a fatal error.
Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.
精彩评论