我试图以静态方法调用在构造函数中声明的属性,但由于没有结果,我认为我做的方法不正确。这是我的代码:
class myClass
{
private static $client;
public function __construct(Client $client)
{
self::$client = $client;
}
public static function getBuses(){
$call = self::$client->get('localhost/api-call');
}
}
这可能是什么问题? 我收到此错误-在null上调用成员函数get()
答案 0 :(得分:-1)
这对我有用。 请为我们提供更多代码,以作为Client类或您执行此代码的方式
<?php
class myClass
{
private static $client;
public function __construct(Client $client)
{
self::$client = $client;
}
public static function getBuses(){
$call = self::$client->get('localhost/api-call');
return $call;
}
}
class Client
{
public function get($string)
{
return $string;
}
}
$client = new Client();
$class = new myClass($client);
echo $class::getBuses();