gpt4 book ai didi

PHP 调用时传递引用是不可避免的吗?

转载 作者:可可西里 更新时间:2023-11-01 00:11:28 24 4
gpt4 key购买 nike

给定以下界面:

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 );
}

FilePoolUserList 都实现了 ISoapInterface

PHP 会提示 foreach 中的两个调用:

Call-time pass-by-reference has been deprecated

所以我查看了该消息,the documentation似乎很清楚如何解决这个问题。从实际调用中删除与号。
所以我将代码更改为如下所示:

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

现在 PHP 报错了

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

除此之外,该功能现已损坏。所以这显然不是解决方案。

最佳答案

来自call_user_func :

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

要调用静态方法,您可以使用 Class::method() 语法,为 Class 和/或 method 部分提供变量:

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

关于PHP 调用时传递引用是不可避免的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3968832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com