tolerant-php-parser icon indicating copy to clipboard operation
tolerant-php-parser copied to clipboard

Incorrectly identified usage of $this in static method

Open kornrunner opened this issue 6 years ago • 0 comments
trafficstars

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

kornrunner avatar Dec 20 '18 15:12 kornrunner