October 11th, 2008
PHP – Call-time pass-by-reference has been deprecated warning work-around
I recently came across a problematic warning within PHP that I felt the need to get to the bottom of, instead of just throwing a band-aid on top of it. This warning stated that “Call-time pass-by-reference has been deprecated” when I was attempting to pass an array to a function by reference within an object oriented class program structure. Since this is a deprecation warning just finding a way of suppressing the warning was not an option as that functionality may be gone at any time. So, after some research I came across a few ways of solving this problem:
Turn off warnings
PHP offers several different ways of turning off error reporting within your code blocks. Both of these solutions will work but, as I started above, deprecation warnings in your code should never be ignored or suppressed as a long term solution as you never know when a future version of PHP will no longer support this functionality. In any event, these methods are:
- Turn off error reporting for the entire program by setting error_reporting(0); at the top of your script. This works but it really isn’t a good idea. If you turn off error reporting you wont be able to see any program control / syntax errors on page.
- Use an error control operator to suppress warnings for that one line of code – this is detailed here: http://us3.php.net/operators.errorcontrol. Basically, if you prepend any statement with the @ operator, error reporting will be suppressed for that statement. Again, you’re just masking the problem.
Allow call-time pass-by-reference in your php.ini file
If you set allow_call_time_pass_reference = on in your php.ini file, this functionality will be allowed in your program structure without those warning messages. Again though, this is a deprecation warning so any functionality to support this may be gone in the next revision.
Embed the object reference within a passed-by-value array
I know this solution is a hack and that PHP should never have deprecated this feature in the first place, but this is at least a functional hack that I could live with. In addition, since this method does not fall in the same deprecation arena as the previous ones, this hack will continue to work even if the deprecated feature is removed. This is the functionality we’ve stuck with:
What we wanted to do with our program is to take some raw friend data obtained from MySpace and convert it into an associative array indexed by user id.
$friendObj = array();
array_walk($this->ownerFriends->Friends, create_function(’$friendObjRaw,$key,$friendObjFinal’,’
$friendObjFinal[0][$friendObjRaw->userId]->image = $friendObjRaw->image;
$friendObjFinal[0][$friendObjRaw->userId]->name = $friendObjRaw->name;
$friendObjFinal[0][$friendObjRaw->userId]->uri = $friendObjRaw->webUri;
‘), array(&$friendObj));
?>
Here are the takeaways from this code block. The way you’ll now pass your object reference in is by wrapping it in a passed-by-value array
array(&$friendObj)
The argument that your function accepts will now be passed by reference
create_function(‘$friendObjRaw,$key,$friendObjFinal‘
When you want to access the passed-by-reference object from within the new function, access it by the array selector:
$friendObjFinal[0]
Happy Coding!
– Jonathan LeBlanc