提问者:小点点

从另一个函数赋值后,我无法访问另一个变量中的变量[closed]


class DigitalAPIClient
{
  private $client = null;
  const API_URL = "http://localhost:8000/api";
  var $auth_key;
  var $auth_secret;
  private $accessToken; -----> This is the variable here

  public function __construct($key, $secret)
  {
    $this->auth_key = $key;
    $this->auth_secret = $secret;
    $this->client = new Client();
  }

  public function prepareAccessToken()
  {
      $url = self::API_URL."/login";
      $data = ['secret_key' => $this->auth_secret, 'client_id' => $this->auth_key];
      $response = $this->client->post($url, ['query' => $data]);
      $result = json_decode($response->getBody()->getContents());
      $this->accessToken = $result->token;  ------> I'm assigning it's value over here
  }

  public function getData()
  {
    $token = $this->accessToken;
    return 'TOKEN:'.$token;
  }
}

变量名为$AccessOken。 我正在从一个名为PrepareAccessToken()的函数中赋值。 我知道这很简单,但我无法解决它。


共1个答案

匿名用户

在php中,默认情况下所有变量都是本地的,要使用全局范围,您需要使用$globals['access token']

引用:PHP保留变量