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( $so开发者_高级运维apProvider 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.
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);
}
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
)...
精彩评论