【问题标题】:Laravel Inheritance FailLaravel 继承失败
【发布时间】:2013-10-23 01:52:19
【问题描述】:

我正在使用 Laravel 4,这里有这段代码:

http://demo.php-pastebin.com/2sfuOUE7

在第一行上方有一行我包含另一个类文件(CHPPConnection,这是一个更容易实现 OAuth 1.0 的库,位于 http://pht.htloto.org

这是该库中的 retrieveAccessToken 方法的代码:

/**
 * Get access token for chpp application
 *
 * @param String $oauthToken
 * @param String $verifier
 */
public function retrieveAccessToken($oauthToken, $verifier)
{
    $params = array(
        'oauth_consumer_key' => $this->consumerKey,
        'oauth_signature_method' => $this->signatureMethod,
        'oauth_timestamp' => $this->getTimestamp(),
        'oauth_nonce' => $this->getNonce(),
        'oauth_token' => $oauthToken,
        'oauth_verifier' => $verifier,
        'oauth_version' => $this->version
    );
    $signature = $this->buildSignature(self::OAUTH_SERVER.self::ACCESS_URL, $params, $this->oauthFirstTokenSecret);
    $params['oauth_signature'] = $signature;
    uksort($params, 'strcmp');
    $url = $this->buildOauthUrl(self::OAUTH_SERVER.self::ACCESS_URL, $params);
    if($this->canLog())
    {
        $this->log("[OAUTH] Access url: ".$url);
    }
    $return = $this->fetchUrl($url, false);
    $result = explode('&', $return);
    foreach($result as $val)
    {
        $t = explode('=', $val);
        $$t[0] = urldecode($t[1]);
    }
    if(isset($oauth_token))
    {
        $this->setOauthToken($oauth_token);
        if($this->canLog())
        {
            $this->log("[OAUTH] Access token: ".$oauth_token);
        }
    }
    if(isset($oauth_token_secret))
    {
        $this->setOauthTokenSecret($oauth_token_secret);
        if($this->canLog())
        {
            $this->log("[OAUTH] Access token secret: ".$oauth_token_secret);
        }
    }
}

为什么我的代码不起作用?为什么 __constructor 方法返回我想要的结果,而 something 方法却没有?在这种情况下,我可能对继承的工作方式有一些错误的理解,所以请帮助我!

【问题讨论】:

    标签: php oauth laravel


    【解决方案1】:

    我认为这可能是因为您试图在构造函数中返回某些内容,所以当您实例化它时,您可能不是检索它的实例而是检索 pht 的实例,显然不会有 something()您正在寻找的功能。

    class PhtController extends BaseController {
    
        protected $_pht;
    
        public function __construct()
        {
                $this->_pht = new CHPPConnection(
                                Config::get("pht.consumerKey"),
                                Config::get("pht.consumerSecret"),
                                Config::get("pht.callback"));
                //this returns true
        }
    
        public function something()
        {
                $at = $this->_pht->retrieveAccessToken($_REQUEST["oauth_token"], $_REQUEST["oauth_verifier"]);
    
               //vardump $at here dumps just NULL and cannot use any other methods aswell, returns false
        }
    }
    
    // If you need to retrieve the instance of pht for any reason, call this function rather than returning it in the constructor.
    public function getPHT()
    {
        return $this->_pht;
    }
    

    【讨论】:

    • 感谢您的努力。不幸的是,这没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多