I've the following PHP method which is part of the codebase which was working fine:
<?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);
}
}
The code was passing all the CI tests before (using phpunit
).
However now when I'm invoking the file via php HooksTest.php
, I've got the following error:
PHP Fatal error: Cannot use
$this
as parameter inHooksTest.php
on line 11Fatal error: Cannot use
$this
as parameter inHooksTest.php
on line 11
I've tested with PHP 7.1, 7.2 and same issue. I believe it was working in PHP 5.6.
How can I convert above code to have the same meaning?
Does removing $this
from the function parameter should be enough?
Just skip $this
argument, change
function ($this) {
return $this;
}
to
function () {
return $this;
}
Look at Example #5 Automatic binding of $this
on Anonymous functions page:
<?php
class Test
{
public function testing()
{
return function() {
var_dump($this);
};
}
}
$object = new Test;
$function = $object->testing();
$function();
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With