我需要在构造函数中创建一个类的实例,然后在我的其他方法中访问它。
我尝试过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('...');
}
}