加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

PHP致命错误:不能使用$this作为参数

发布时间:2021-07-06 05:22:13 所属栏目:大数据 来源: https://www.jb51.cc
导读:我有以下PHP方法,它是代码库的一部分,工作正常: ?PHPclass HooksTest extends DrupalTestCase { public function testPageAlterIsLoggedIn() { $this-drupal-shouldReceive('userIsLoggedIn') -once() -andReturn(TRUE); $this-drupal-shouldReceive('drupa

我有以下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 in HooksTest.PHP on line 11

Fatal error: Cannot use $this as parameter in HooksTest.PHP on line 11

我已经使用PHP 7.1,7.2和同样的问题进行了测试.我相信它在PHP 5.6中运行.

如何将上面的代码转换为具有相同的含义?

从函数参数中删除$this应该足够吗?

解决方法:

只要跳过$this这个论点,就改变吧

function ($this) {
    return $this;
}

function () {
    return $this;
}

查看示例#5在Anonymous functions页面上自动绑定$this:

<?PHP
class Test
{
    public function testing()
    {
        return function() {
            var_dump($this);
        };
    }
}
$object = new Test;
$function = $object->testing();
$function();
?>

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读