Skip to content

Call to static method on trait #4107

@dingo-d

Description

@dingo-d

Support question

I'm trying to understand why is PHPStan throwing the error on this code

<?php

declare(strict_types=1);

trait A
{
    public static function bla(): string
    {
        return 'bla';
    }
}

function bar(): string
{
    return A::bla();
}


function foo(): string
{
    return (new class { use A; } )::bla();
}


echo bar();
echo foo();
Line 15:   Call to static method bla() on trait A.

So in the case of

function bar(): string
{
    return A::bla();
}

I will get an error but in case of

function foo(): string
{
    return (new class { use A; } )::bla();
}

There is no error.

How come if both will work just fine? The same happens if I'm in a class context and use a static method from a trait in another method directly using the name of the trait.

https://3v4l.org/DDDrq

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions