Phpstan: Call to static method on trait

Created on 20 Nov 2020  路  3Comments  路  Source: phpstan/phpstan

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

bug

Most helpful comment

Today I learnt you can call static method on a trait...

All 3 comments

Today I learnt you can call static method on a trait...

Was this page helpful?
0 / 5 - 0 ratings