PHP为什么我在回显时得到相同的值?

时间:2014-04-11 06:46:49

标签: php yii google-analytics

    public $client;
    public $youtubeClient;

    public function init()
    {
        Yii::import('ext.googleapi.src.*');
        require_once 'Google_Client.php';
        require_once 'contrib/Google_AnalyticsService.php';

        $this->client = new Google_Client();
        $this->client->setApplicationName(Yii::app()->params['applicationName']);

        $this->client->setClientId(Yii::app()->params['client_id']);
        $this->client->setClientSecret(Yii::app()->params['client_secret']);
        $this->client->setRedirectUri(Yii::app()->params['redirect_uri']);
        $this->client->setDeveloperKey(Yii::app()->params['developer_key']);
        $this->client->setScopes(array(Yii::app()->params['scopes']));
        $this->client->setUseObjects(true);

    /*** Youtube Connect Code ***/
    require_once 'contrib/Google_YouTubeAnalyticsService.php';
    require_once 'contrib/Google_YouTubeService.php';

    $this->youtubeClient = new Google_Client();
    $this->youtubeClient->setClientId(Yii::app()->params['youtube_client_id']);
    $this->youtubeClient->setClientSecret(Yii::app()->params['youtube_client_secret']);
    $this->youtubeClient->setScopes(array(
      'https://www.googleapis.com/auth/yt-analytics.readonly',
      'https://www.googleapis.com/auth/youtube.readonly'
    ));
    $this->youtubeClient->setDeveloperKey(Yii::app()->params['developer_key']);
    $redirect = filter_var(Yii::app()->params['youtube_redirect_uri'],FILTER_SANITIZE_URL);
    $this->youtubeClient->setRedirectUri($redirect);
    $this->youtubeClient->setState(mt_rand());
    $this->youtubeClient->setUseObjects(true);

    echo "Client Secret: ".$this->client->getClientSecret();
    echo "<br>";
    echo "Youtube Secret: ".$this->youtubeClient->getClientSecret();
    die;
  }

我将Client Secret和Youtube Secret设置为具有不同的值,但我想知道为什么当我尝试回显它们时它们具有相同的值。

结果如下:

Client Secret: aaaa-bbb-ccc
Youtube Secret: aaa-bbb-ccc

这是因为我使用了同一个班级吗?比如代码new Google_Client()。我不确定我是否能以正确的方式做到这一点。有什么想法吗?谢谢! :)

1 个答案:

答案 0 :(得分:0)

正如您在Google_Client source

中所看到的
public function setClientSecret($clientSecret) {
    global $apiConfig;
    $apiConfig['oauth2_client_secret'] = $clientSecret;
    self::$auth->clientSecret = $clientSecret;
}

它将值存储在静态字段中。