Skip to content

Incorrectly identified usage of $this in static method #273

@kornrunner

Description

@kornrunner

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions