PHP Warning: Call-Time Pass-By-Reference Has Been Deprecated

PHP warning: Call-time pass-by-reference has been deprecated

Remove & from &$this everywhere, it is not needed. In fact, I think you can remove & everywhere in this code - it is not needed at all.

Long explanation

PHP allows to pass variables in two ways: "by value" and "by reference". First way ("by value"), you can't modify them, other second way ("by reference") you can:

     function not_modified($x) { $x = $x+1; }
function modified(&$x) { $x = $x+1; }

Note the & sign. If I call modified on a variable, it will be modified, if I call not_modified, after it returns the value of the argument will be the same.

Older version of PHP allowed to simulate behavior of modified with not_modified by doing this: not_modified(&$x). This is "call-time pass by reference". It is deprecated and should never be used.

Additionally, in very ancient PHP versions (read: PHP 4 and before), if you modify objects, you should pass it by reference, thus the use of &$this. This is neither necessary nor recommended anymore, as object are always modified when passed to function, i.e. this works:

   function obj_modified($obj) { $obj->x = $obj->x+1; }

This would modify $obj->x even though it formally is passed "by value", but what is passed is object handle (like in Java, etc.) and not the copy of the object, as it was in PHP 4.

This means, unless you're doing something weird, you almost never need to pass object (and thus $this by reference, be it call-time or otherwise). In particular, your code doesn't need it.

PHP Fatal error: Call-time pass-by-reference has been removed

You are trying to pass a pointer to your array in array_push. That is why the fatal error is encountered. Simply use:

array_push( $image_set, $images[fname] );

Note: array_push() will raise a warning if the first argument is not
an array.

PHP 5.4 Call-time pass-by-reference - Easy fix available?

You should be denoting the call by reference in the function definition, not the actual call. Since PHP started showing the deprecation errors in version 5.3, I would say it would be a good idea to rewrite the code.

From the documentation:

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);.

For example, instead of using:

// Wrong way!
myFunc(&$arg); # Deprecated pass-by-reference argument
function myFunc($arg) { }


// Right way!
myFunc($var); # pass-by-value argument
function myFunc(&$arg) { }

Call-time pass-by-reference has been deprecated

Remove the & before $this.

PHP5 doesn't need that added - all objects are passed as object identifiers by default, no need to mimic this with passing by reference as it was required for PHP 4.

Call-time pass-by-reference has been removed

Only call time pass-by-reference is removed. So change:

call_user_func($func, &$this, &$client ...

To this:

call_user_func($func, $this, $client ...

&$this should never be needed after PHP4 anyway period.

If you absolutely need $client to be passed by reference, update the function ($func) signature instead (function func(&$client) {)

PHP 5.4 Call-time pass-by-reference - Easy fix here?

Just remove the leading & like this:

$fp = fsockopen($url['host'], "80", $errno, $errstr, $timeout);

the variables are still passed by reference, but you don't need the & to indicate that from PHP 5.4 onwards.

Related Topics

Leave a reply