【发布时间】:2013-06-09 20:09:27
【问题描述】:
当我运行这段代码时
class Kernel
{
private $settings = array();
public function handle(Settings $conf)
{
$this->settings = $conf;
return $this;
}
public function run()
{
var_dump($this->settings);
}
}
class Settings
{
public static function appConfig()
{
return array(
'database' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => 'test',
'database' => 'testdb'
)
);
}
}
$kernel = new Kernel;
$kernel->handle(Settings::appConfig())->run();
我得到错误
Catchable fatal error: Argument 1 passed to Kernel::handle() must be an instance of Settings, array given, called in....
这是否意味着类型提示仅适用于实例而不适用于静态方法?如果现在如何实现静态方法的类型提示?
【问题讨论】:
-
handle()需要一个Settings类的对象,但您只是提供了一个简单的数组(appConfig()的返回值) -
appConfig的返回值不是类Settings的实例,不能给想要获取类型设置的方法一个类型数组
标签: php class oop static-methods