【发布时间】:2015-11-11 12:35:32
【问题描述】:
小问题:$this 在范围内的行为是否从 PHP 5.3.29 更改为 5.5.24?我在PHP 5 Changelog 中找不到任何相关更改。
详情: 在this question 中,我认为我有一个解决问题的方法(在 PHPUnit 中,期望以数组为参数的方法调用)。这是我的解决方案
public function test_actionUpload_v10MasterdataFile()
{
/*
* Create a stub to disable the original constructor.
* Exposing data and rendering are stubbed.
* All other methods behave exactly the same as in the real Controller.
*/
$sut = $this->getMockBuilder('MasterdataController')
->setMethods(array('exposeAndSaveDataLines', 'render'))
->disableOriginalConstructor()
->getMock();
$sut->expects($this->once())
->method('exposeAndSaveDataLines')
->will($this->returnCallback(function($lines) {
$expectedLines = include ($this->dataDir() . 'ExpectedLines.php');
PHPUnit_Framework_Assert::assertTrue($this->similar_arrays($lines, $expectedLines));
}));
// Execute the test
$sut->actionUpload();
}
它在我的本地环境(PHP 5.5.24,Zend Engine v2.5.0)上运行,但是当我将代码复制到我们的测试服务器(PHP 5.3.29,Zend Engine v2.3.0)时,这不起作用,由于这一行:
$expectedLines = include ($this->dataDir() . 'ExpectedLines.php');
错误是:
Using $this when not in object context
这可能是由于 PHP 版本,还是我应该在其他地方寻找它在一台服务器上失败但在另一台服务器上失败的原因?
【问题讨论】:
-
你在闭包中使用
$this,显然它不会从调用范围继承任何变量。
标签: php zend-framework phpunit this