我这是工作正常的代码库的一部分,下面的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 Fatal error: Cannot use
$this
as parameter inHooksTest.php
on line 11Fatal error: Cannot use
$this
as parameter inHooksTest.php
on line 11
我用PHP 7.1,7.2和相同的问题进行测试。我相信它在PHP 5.6中工作。
如何将上面的代码转换为具有相同的含义?
从函数参数中删除$this
应该足够吗?
使用self :: maybe? – rak007
我不认为你可以将'$ this'传递给' - >和返回函数中的函数(function($ this){' – aynber
这是通过所有测试的现有代码的一部分,所以这不是我的发明。我知道它工作正常,现在在PHP 7.x下失败了。 – kenorb