Example:
<?php
class Foo {
private $bar = 'baz';
}
class Test {
public static function run () {
$getter = function () { return $this->bar; };
$closure = Closure::bind($getter, new Foo(), Foo::class);
return $closure();
}
}
echo Test::run() . "\r\n";
Reports error $this can not be used in static methods.:
{
"resource": "/closure.php",
"owner": "_generated_diagnostic_collection_name_#2",
"severity": 8,
"message": "$this can not be used in static methods.",
"source": "php",
"startLineNumber": 9,
"startColumn": 41,
"endLineNumber": 9,
"endColumn": 46
}
Code itself is valid:
$ php -l closure.php
No syntax errors detected in closure.php
$ php closure.php
baz
Example:
Reports error
$this can not be used in static methods.:{ "resource": "/closure.php", "owner": "_generated_diagnostic_collection_name_#2", "severity": 8, "message": "$this can not be used in static methods.", "source": "php", "startLineNumber": 9, "startColumn": 41, "endLineNumber": 9, "endColumn": 46 }Code itself is valid:
$ php -l closure.php No syntax errors detected in closure.php $ php closure.php baz