【问题标题】:how set parameters by reference in nusoap?如何在nusoap中通过引用设置参数?
【发布时间】:2012-06-02 13:53:56
【问题描述】:

如何在 nusoap.xml 中通过 ref 设置参数。在下面的代码中,我应该通过 ref 设置两个参数(status 和 recId)。请注意& 不起作用:

        $params = array(
        'username' => GATEWAY_USERNAME,
        'password' => GATEWAY_PASSWORD,
        'from' => GATEWAY_NUMBER,
        'to' => array($to),
        'text' => $message,
        'flash' => $flash,
        'udh' => '',
        'status' => &$status,
        'recId' => &$recId
    );

    $sendParams=array($params);
    $res=$this->client->call('Send',$sendParams);

【问题讨论】:

  • statusrecId 原始类型吗?
  • 试试$res=$this->client->call('Send', &$sendParams);
  • 好的,我对这个非常古老的问题给予了赏金,因为我无法在任何地方找到答案。我正在尝试做的几乎与 OP 相同。我试图通过引用传递一个字符串并将一个值放入函数内部。不,放 & 没有用。
  • & 放入原始值不起作用。你应该改用一个类
  • 你想用这个实现什么?您是否认为函数调用正在改变这些值?你能再解释一下吗?

标签: php nusoap ref


【解决方案1】:

关于通过引用传递值:

function test_reference_in(&$array) {
    $array['a'] = 2;
}

$test_array['a'] = 1;
test_reference_in($test_array);
echo $test_array; //-> it prints 2

关于nusoap:

现在在nusoap 中,客户端类是nusoap_client
在该课程中,您有nusoap_client::call() 拨打肥皂电话。

这就是nusoap_client::call() 中出现的数组$params 中的内容,在您的示例中就是您的$sendParams
我将省略与$params 无关的所有其余方法,以更好地解释发生了什么。

/**
 * Pseudocode of call to explain the operations on $params
 * @see nusoap_client::call()
 */
function call($operation,$params=array(),$namespace='http://tempuri.org',$soapAction='',$headers=false,$rpcParams=null,$style='rpc',$use='encoded'){
    /*
       some code 
       .
       .
       .

     */
    // varDump is just var_dump so print $params 
    $this->appendDebug('params=' . $this->varDump($params));
     /*
       some code 
       .
       .
       .

     */ 

    /*
     * Here $params has been read, no write operation in there 
     * about serializeRPCParameters @see class.wsdl.php again is just reading
     */
    if (is_string($params)) {
        $this->debug("serializing param string for WSDL operation $operation");
        $payload = $params;
    } elseif (is_array($params)) {
        $this->debug("serializing param array for WSDL operation $operation");
        $payload = $this->wsdl->serializeRPCParameters($operation,'input',$params,$this->bindingType);
    } else {
        $this->debug('params must be array or string');
        $this->setError('params must be array or string');
        return false;
    }

    /*
     * Here again $params has been read, no write operation in there 
     */
    if (is_string($params)) {
        $this->debug("serializing param string for operation $operation");
        $payload = $params;
    } elseif (is_array($params)) {
        $this->debug("serializing param array for operation $operation");
        foreach($params as $k => $v){
            $payload .= $this->serialize_val($v,$k,false,false,false,false,$use);
        }
    } else {
        $this->debug('params must be array or string');
        $this->setError('params must be array or string');
        return false;
    }
}   

因此,正如您在此处看到的,通过引用传递 $params 没有任何优势。

因为:

  1. $params 在 nusoap_client::call() 中没有以任何方式修改
  2. 即使您在必须再次使用 nusoap_client::call() 之后在其他地方修改 $params

如果你想通过引用传递 $params 怎么办?我可以吗?

是的,你可以!
要实现这一点,您必须复制 nusoap_client.php 并将其命名为 nusoap_client_new.php
在那里将类名形式nusoap_client修改为nusoap_client_new

// From this 
class nusoap_client extends nusoap_base  {

// To this 
class nusoap_client_new extends nusoap_base  {

修改nusoap_client_new::call() 方法,在参数中添加ref,如下所示:

/*
 * Please note &$params=array() instead of $params=array()
 */
function call($operation,&$params = array(),$namespace='http://tempuri.org',$soapAction='',$headers=false,$rpcParams=null,$style='rpc',$use='encoded'){
    /**
     * Of course here you have to modify your code to make some operation on $params
     * according to your needs.
     */
     /*
       original code 
       .
       .
       .

     */ 
}

最后更新您的代码以要求并使用nusoap_client_new::call() 而不是nusoap_client::call()

【讨论】:

    【解决方案2】:

    古斯塔沃专用。

    PHP 5.4的版本升级:

    调用时通过引用传递已被删除。

    这是什么意思,在<= PHP 5.3 版本中,这样的代码应该可以工作:

    function appendA($string) {
        $stringA .= 'A';
    }
    
    $string = '';
    appendA(&$string);
    

    但以后不再工作。为了让 PHP 5.4+ 创建一个通过引用编辑其参数的函数,您必须从一开始就以这种方式声明它。例如:

    function appendA(&$string) { // <- first change
        $stringA .= 'A';
    }
    
    $string = '';
    appendA($string); // <- second change
    

    请注意,在 PHP 5.4+ 中,尝试使用 appendA(&amp;$string) 调用函数将产生通知,并且不会通过引用更改值。

    根据原始问题,我还没有找到在 nusoap 的 client::call() 函数期间更改 $params 的地方,所以我认为通过引用提供它们没有意义,但我可能是盲目的。无论如何,方法未以与 PHP 5.4+ 兼容的方式声明,因此方法调用期间的 &amp; near 参数不适用于那些 PHP 版本。在这种情况下需要修改方法。

    【讨论】:

      猜你喜欢
      • 2017-12-07
      • 1970-01-01
      • 2014-04-27
      • 2020-01-16
      • 2012-11-14
      • 2020-09-09
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多