是否可以$measurements在测试用例中访问该属性?
class Performance
{
private static $measurements = [];
public static function createMeasurement()
{
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试了这样的事情:
$reflection = new \ReflectionClass(get_class(Performance));
$property = $reflection->getProperty('measurements');
$property->setAccessible(true);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为Use of undefined constant Performance - assumed 'Performance'. 如何告诉 php 从静态类中获取对象?
您还可以通过闭包访问私有属性。您只需将它们绑定到类范围即可。
$getMeasurements = function() { return static::$measurements; };
$getPerformanceMeasurements = $getMeasurements->bindTo(null, Performance::class);
$measurements = $getPerformanceMeasurements();
Run Code Online (Sandbox Code Playgroud)