【问题标题】:PHP OOP - setting class parameter, after an external class callback from Zebra_CurlPHP OOP - 在 Zebra_Curl 的外部类回调之后设置类参数
【发布时间】:2013-11-20 17:58:46
【问题描述】:

我正在使用这个库:http://stefangabos.ro/php-libraries/zebra-curl/

在我自己的类结构中检索令牌。

<?php
class getToken 
{
    private $url =  "";
    private $customHeaders = array('Content-Type: application/x-www-form-urlencoded',);
    private $parameters = array();
    private $cacheTime = 600;
    private $curl;
    public  $token = false;

    public function __construct(){
        require_once('lib/Zebra_cURL.php');
        $this->curl = new Zebra_cURL();
        $this->curl->cache('cache', $this->cacheTime);
        $this->curl->option(CURLOPT_HTTPHEADER, $this->customHeaders);
        $this->curl->post($this->url, $this->parameters, __CLASS__.'::tokenCallback');
    }    

    public function returnToken(){
        return $this->token;
    }

    public function tokenCallback($result){
        if ($result->response[1] == CURLE_OK) {
            $body = str_replace(array('\\/','\"'),array('/','"'), html_entity_decode($result->body)); 
            $tokenStart = stripos($body,"<EncryptedAssertion");
            $tokenEnd= stripos($body,"EncryptedAssertion>");
            $token = substr($body, $tokenStart, $tokenEnd-$tokenStart);
            $this->$token = $token;
        }else{
            $this->token = false;
        }
    }
}

在tokenCallback 方法中,我试图设置类$token 参数的值,但是得到以下错误:致命错误:使用$this when not in object context in ....

我尝试使用 self 进行设置,但出现此错误:致命错误:访问未声明的静态属性:getToken::$token

我猜想来自 Zebra_Curl 的回调被实例化的方式是问题所在 - 有人知道如何让它工作吗?

【问题讨论】:

    标签: php class oop curl scope


    【解决方案1】:

    据我所知,您在 Zebra curl 的回调中调用了一个静态函数。您的方法是使用实​​例化变量“令牌”。静态调用的问题是它不会保持当前状态,并且会设置任何你可以为类定义的全局静态变量。我会尝试以下方法:array($this, 'tokenCallback')。

    我没有看过 Zebra curl,但如果他正在调用 call_user_func,这应该对你有用。

    public function __construct(){
        require_once('lib/Zebra_cURL.php');
        $this->curl = new Zebra_cURL();
        $this->curl->cache('cache', $this->cacheTime);
        $this->curl->option(CURLOPT_HTTPHEADER, $this->customHeaders);
        $this->curl->post($this->url, $this->parameters, array($this,'tokenCallback') );
    }    
    
    public function returnToken(){
        return $this->token;
    }
    
    public function tokenCallback($result){
        if ($result->response[1] == CURLE_OK) {
            $body = str_replace(array('\\/','\"'),array('/','"'), html_entity_decode($result->body)); 
            $tokenStart = stripos($body,"<EncryptedAssertion");
            $tokenEnd= stripos($body,"EncryptedAssertion>");
            $token = substr($body, $tokenStart, $tokenEnd-$tokenStart);
            $this->$token = $token;
        }else{
            $this->token = false;
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2013-09-28
      • 1970-01-01
      • 2011-10-11
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多