Skip to content

Cannot override trait method #2935

@rosasurfer

Description

@rosasurfer

There already are a few issues discussing using trait and class inheritance together but it seems to me none of them really fit my observation (I may be wrong) .

I'm trying to override a trait method the way the official PHP documentation shows but PHPStan complains. Please see the example:
https://phpstan.org/r/3cd9314c-002d-4796-b9ce-4715c57650da

On 3v4l.org the code executes just fine. Any hints? Thank you.

class A {
}
class B extends A {
    public function bar() : bool {
        return true;
    }
}
trait T {
    public function bar() : bool {
        return parent::bar();
    }
}
class C extends A {
    use T;

    public function bar() : bool {
        return false;
    }
}

var_dump((new C())->bar());

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions