Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP致命错误:无法使用$this作为参数_Php_This_Fatal Error - Fatal编程技术网

PHP致命错误:无法使用$this作为参数

PHP致命错误:无法使用$this作为参数,php,this,fatal-error,Php,This,Fatal Error,我有以下PHP方法,它是代码库的一部分,工作正常: <?php class HooksTest extends DrupalTestCase { public function testPageAlterIsLoggedIn() { $this->drupal->shouldReceive('userIsLoggedIn') ->once() ->andReturn(TRUE); $this->drupal->

我有以下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);
  }
}

跳过
$this
参数,更改

function ($this) {
    return $this;
}


请参见第页的示例#5自动绑定
$this



是的,正如aynber之前所说,您不能将$this作为函数参数传入。通过这样做,你基本上是在重新定义它。您只需将其作为函数参数删除。

如果希望其工作方式与以前相同,则不应将
$this
作为参数删除。您应该将参数的名称更改为其他名称,并在闭包中更改相应的变量名称

通过PHP5.6,在类方法的闭包中使用
$this
作为参数将屏蔽引用父对象的
$this
。例如:

class Example
{
    public function test ($param) {
        $closure = function ($whatever) {      // $this not used as parameter
            return $this;                      // $this refers to the Example object
        };
        return $closure($param);
    }

    public function test2 ($param) {
        $closure = function($this) {          // $this used as parameter
            return $this;                     // $this no longer refers to Example object
        };
        return $closure($param);
    }

}

$ex = new Example;
$not_masked = $ex->test('foo');
$masked = $ex->test2('foo');

var_dump($not_masked, $masked);

在PHP5.6中,
$masked
将是字符串“foo”,而不是
$ex
对象

根据您在3v4l.org链接上看到的不同版本的输出,在7.0.0-7.0.6之间有一段短暂的时间,
$this
参数显然会被忽略,以支持对象自参考。我假设他们不允许在以后的版本中使用
$this
作为参数,以避免在闭包范围中实际引用的
$this
不明确

看起来这只是混淆了原始代码中的命名。为了使其像以前一样工作:

->andReturnUsing(function ($anyOtherName) {
    return $anyOtherName;
});

使用self::也许吧?我认为您无法将
$this
传递到
->中的函数中,并返回使用(函数($this){
这是通过所有测试的现有代码的一部分,因此它不是我的发明。我知道它工作正常,但在PHP7.x下失败了。问题似乎是使用
$This
作为参数名称会在参数和引用objec的
$This
之间产生冲突t在功能范围内。看起来这是在7.1中实现的更改:
class Example
{
    public function test ($param) {
        $closure = function ($whatever) {      // $this not used as parameter
            return $this;                      // $this refers to the Example object
        };
        return $closure($param);
    }

    public function test2 ($param) {
        $closure = function($this) {          // $this used as parameter
            return $this;                     // $this no longer refers to Example object
        };
        return $closure($param);
    }

}

$ex = new Example;
$not_masked = $ex->test('foo');
$masked = $ex->test2('foo');

var_dump($not_masked, $masked);
->andReturnUsing(function ($anyOtherName) {
    return $anyOtherName;
});