Skip to content

Override static in return types with self in final classes #17725

Closed
@rekmixa

Description

@rekmixa

Description

After implementing static types in return methods, I noticed one thing that was illogical, in my opinion.

We have the following code:

<?php

declare(strict_types=1);

interface A
{
    public function method1(): static;
}

abstract class B
{
    abstract public function method2(): static;
}

trait C
{
    abstract public function method3(): static;
}

final class Foo extends B implements A
{
    use C;

    public function method1(): static
    {
        return $this;
    }

    public function method2(): static
    {
        return $this;
    }

    public function method3(): static
    {
        return $this;
    }
}

$foo = new Foo();

var_dump($foo->method1());
var_dump($foo->method2());
var_dump($foo->method3());

It seems to me that in final classes, redefining return types in methods from static to self will not be a violation of covariance, because the final class will never have children. But now it triggers an error Fatal error: Declaration of Foo::method2(): Foo must be compatible with B::method2(): static. What do you think about this?

final class Foo extends B implements A
{
    use C;

    public function method1(): self
    {
        return $this;
    }

    public function method2(): self
    {
        return $this;
    }

    public function method3(): self
    {
        return $this;
    }
}

In addition, the same PHPStorm suggests replacing static types with self types in final classes (if this type is not specified in the prototype).

Below I attach a link to the pull request in which I implemented this logic.

#17724

  • There are no changes that break backward compatibility or covariance logic.

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