也许是在清晨,或者我完全失明了,但为什么我在下面的代码中收到“致命错误:不在对象上下文中时使用 $this”。里面没有任何静态的东西。
班上:
<?php
class Property
{
/**
* @var string[]
*/
private $values;
public function __contruct($file)
{
$this->values = parse_ini_file($file, false);
}
/**
* @return string
*/
public function get($key, $default = null)
{
if (key_exists($key, $this->values)) { //<-- error
return $this->values[$key];
}
return $default;
}
}
考试:
<?php
class PropertyTest extends Test
{
public function testGet()
{
$prop = new Property($this->getResource('test.properties'));
$this->assertEquals('foo', $prop->get('test.entry', 'xyz'));
$this->assertEquals('bar', $prop->get('test.entry2', 'xyz'));
$this->assertEquals('xyz', $prop->get('test.entry3', 'xyz'));
$this->assertEquals(null, $prop->get('test.entry3'));
}
}
Edit
指示跟踪的错误注释。在第一个 $prop->get() 上运行 PropertyTest->testGet() 时发生错误,由 Property->get() 方法的第一行引起。
Solution
除了 xdazz 发现的拼写错误和 Phil 指出的弃用之外,user985935 是正确的。 :)
简而言之,我的类加载器使用了静态 get ,并且 phpUnit 错误误导了我的调查以及我为您提供的用于查找问题的信息。对不起。