PHP 调用时传递引用不可避免? [英] PHP Call-time pass-by-reference unavoidable?
问题描述
给定以下界面:
interface ISoapInterface {
public static function registerSoapTypes( &$wsdl );
public static function registerSoapOperations( &$server );
}
以及以下代码:
$soapProvider = array( "FilePool", "UserList" );
foreach( $soapProvider as $provider ) {
call_user_func( array( $provider, "registerSoapTypes" ), &$server->wsdl );
call_user_func( array( $provider, "registerSoapOperations" ), &$server );
}
FilePool
和 UserList
都实现了 ISoapInterface
.
PHP 会抱怨 foreach 中的两个调用声明:
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 );
}
现在 PHP 抱怨
FilePool::registerSoapTypes 的参数 1 应为参考,给定值
FilePool::registerSoapOperations 的参数 1 应为参考,给定值
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.
推荐答案
来自 call_user_func
:
注意 call_user_func() 的参数不是通过引用传递的.
Note that the parameters for call_user_func() are not passed by reference.
要调用静态方法,您可以使用 Class::method()
语法,为 Class
和/或 method
部分提供一个变量:
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);
}
这篇关于PHP 调用时传递引用不可避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!