如何从类方法访问属性


How do I access a property from a class method?

我需要在构造函数中创建一个类的实例,然后在我的其他方法中访问它。

我尝试过init函数和constructor都,但没有运气(因为我是OOP概念的新手)

private $client;
// first I tried this
public function __construct(){
    $this->client = new 'GuzzleHttp'Client();
}
// then I tried this
public function init(){
    $this->client = new 'GuzzleHttp'Client();
    // I also tried that
    // $client = new 'GuzzleHttp'Client();
}
/**
 * [xyz description]
 * @return [void]
 */
public function xyz(){
    // I need to use that client variable here
}

如何在我的xyz方法和同类的其他方法中使用$client

通过$this->client访问它,就像在构造函数中那样:

class Foo
{
    private $client;
    public function __construct()
    {
        $this->client = new 'GuzzleHttp'Client();
    }
    public function xyz()
    {
        $this->client->get('...');
    }
}