【发布时间】:2018-03-03 01:32:08
【问题描述】:
我有以下 PHP 方法,它是运行良好的代码库的一部分:
<?php
class HooksTest extends DrupalTestCase {
public function testPageAlterIsLoggedIn() {
$this->drupal->shouldReceive('userIsLoggedIn')
->once()
->andReturn(TRUE);
$this->drupal->shouldReceive('drupalPageIsCacheable')
->once()
->andReturnUsing(function ($this) {
return $this;
});
$page = [];
$cacheable = $this->object->pageAlter($page);
$this->assertFalse($cacheable);
}
}
该代码之前通过了所有 CI 测试(使用 phpunit)。
但是现在当我通过php HooksTest.php 调用文件时,出现以下错误:
PHP 致命错误:无法在第 11 行的
HooksTest.php中使用$this作为参数致命错误:无法在第 11 行的
HooksTest.php中使用$this作为参数
我已经测试过 PHP 7.1、7.2 和相同的问题。我相信它在 PHP 5.6 中工作。
如何将上面的代码转换为相同的含义?
从函数参数中删除$this就足够了吗?
【问题讨论】:
-
使用 self:: 也许?
-
我认为你不能将
$this传递给->andReturnUsing(function ($this) {中的函数 -
这是通过所有测试的现有代码的一部分,所以它不是我的发明。我知道它工作正常,现在在 PHP 7.x 下失败了。
-
看起来问题是使用
$this作为参数的名称会在参数和引用函数范围内的对象的$this之间产生冲突。 -
看起来这是在 7.1 中实现的更改:wiki.php.net/rfc/this_var#disable_using_this_as_parameter
标签: php this fatal-error