在静态方法中调用属性时出错



我正在尝试以静态方法调用构造函数中声明的属性,但我认为我这样做的方式不正确,因为我没有得到任何结果。这是我的代码:

class myClass
{
private static $client;
public function __construct(Client $client)
{
self::$client = $client;
}
public static function getBuses(){
$call = self::$client->get('localhost/api-call');
}
}

这里可能出了什么问题? 我收到此错误 - 在空值上调用成员函数 get((

这对我有用。 请给我们更多代码作为客户端类使用,或者你执行它的方式

<?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();

相关内容

最新更新