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.

 

<?php
$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

  • Share/Bookmark


MySpace PHP API – 20 friend maximum return bug fix


While working with the MySpace official and unofficial php api libraries I became frustrated with one slight bug that was causing some pains for my application. I want to note first off that both of these libraries have been great tools to save from having to use the front end javascript api library that OpenSocial defines.

For our needs, we only needed 3 things from the library…the owner profile data, viewer profile data and the owner friend data. The problem here was that no matter what arguments I supplied to the built in functions I could never get back more than 20 friends, even though the standard specifies that it allows for a return of ‘all’ friends and that 20 was just a default. So, I started hacking away at the API and OAuth.php file of the unofficial library to see if I could find a solution.

What was the problem and how was it fixed?
What it looked like is that the OAuth request for friend profile data is temperamental when it comes to the order of the parameters being passed in the query string. It appears that the ‘page_size’ parameter has to be the first parameter passed after the request URI in order for the parameters to have any effect (i.e. http://api.myspace.com/v1/users/{USERID}/friends?page_size=40…). This parameter controls how many user profiles to send back through the request and can be set to ‘all’ for all of your friends’ profiles. So, I went into the OAuth.php file and switched the order that the OAuth and function parameter arrays were being merged. There was a slight issue in the friend request function in the unofficial php api (the array values were not passing correctly to the request function) so I adjusted those features and set up that argument array so that page_size would be the first element passed in to the request.

Where is the fix?
File: http://www.nakedtechnologist.com/files/ms_unofficial_php_api_with_friend_fix.zip

All you need to do is download the zip file, unzip it and replace the unofficial library files. The zip contains all of the files that were in the original API. To do a quick setup to display friend data, you can use the following PHP code:

<?php
require_once(’Space.php’);
$key = ‘http://www.myspace.com/xxxxx’;
$secret = ‘xxxxxxxxxxx’;
$session = new Space($key, $secret);
$friend_list = $session->friends(’USER_ID′,1,’all’);
var_dump($friend_list);
?>

I saw many questions on the forums regarding what the xxxxx portion of the $key parameter is. This is the friend id of your application, which may be viewed on the add application page (e.g. http://www.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=396659585).

– Jonathan LeBlanc

  • Share/Bookmark