Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Call-time pass-by-reference unavoidable?

Given the following interface:

interface ISoapInterface {
  public static function registerSoapTypes( &$wsdl );
  public static function registerSoapOperations( &$server );
}

And the following code:

$soapProvider = array( "FilePool", "UserList" );
foreach( $soapProvider as $provider ) {
  call_user_func( array( $provider, "registerSoapTypes" ), &$server->wsdl );
  call_user_func( array( $provider, "registerSoapOperations" ), &$server );
}

FilePool and UserList both implement ISoapInterface.

PHP will complain about the two calls inside the foreach stating:

Call-time pass-by-reference has been deprecated

So I looked that message up, and the documentation seems quite clear on how to resolve this. Removing the ampersand from the actual call.
So I changed my code to look like this:

$soapProvider = array( "FilePool", "UserList" );
foreach( $soapProvider as $provider ) {
  call_user_func( array( $provider, "registerSoapTypes" ), $server->wsdl );
  call_user_func( array( $provider, "registerSoapOperations" ), $server );
}

Now PHP complains

Parameter 1 to FilePool::registerSoapTypes expected to be reference, value given
Parameter 1 to FilePool::registerSoapOperations expected to be reference, value given

In addition to that, the functionality is now broken. So this obviously can't be the solution.

like image 216
Oliver Salzburg Avatar asked May 31 '26 02:05

Oliver Salzburg


2 Answers

From the call_user_func:

Note that the parameters for call_user_func() are not passed by reference.

To invoke static methods you can use Class::method() syntax, supplying a variable for the Class and/or method parts:

$soapProvider = array( "FilePool", "UserList" );
foreach( $soapProvider as $provider ) {
  $provider::registerSoapTypes($server->wsdl);
  $provider::registerSoapOperations($server);
}
like image 181
meagar Avatar answered Jun 01 '26 15:06

meagar


While call_user_func does not pass parameters by reference, call_user_func_array can.

$callback = array($provider, 'blahblahblah');
call_user_func_array($callback, array( &$server ));

The only real difference is that it expects an array of parameters instead of a list of parameters like call_user_func (similar to the difference between sprintf and vsprintf)...

like image 28
ircmaxell Avatar answered Jun 01 '26 14:06

ircmaxell