Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return type "self" in abstract PHP class

PHP 7.1

I'm currently trying to make an abstract class to provide and define and partially implement functionality of its' child classes.

Here I use the following construct:

abstract class Parent {

    public static function fromDB(string $name = '') {
        $instance = new static();
        if (!empty($name)) {
            $instance->setName($name)->read();
        }
        return $instance;
    }

    public abstract function read();

    public abstract function setName(string $name): self;

}

Here PHP seems to understand that setName($name) returns a Object with type Parent, but PhpStorm indicates that read() can not be called on the result, which would have been the expected result.

Error Message: Referenced Method is not found in subject class.

I'm not sure if this is a bug in PHP or PhpStorm, or, much more likely, me not understanding what I'm doing...

I've read up on Late static binding and the following questions which partially talk about this problem, but I couldn't figure out how to fix it:

  • Question 1
  • Question 2
  • Late Static Binding Doc

Thank you for your time and help.


EDIT: As hinted below, I'm trying to implement in child classes like:

public function setName(string $name = null): user {...}

which obviously doesn't work with self return but (IMO should) with static, which is forbidden.

like image 327
Simon Avatar asked Jun 28 '17 13:06

Simon


People also ask

Can an abstract class be a return type?

An abstract method has no body. (It has no statements.) It declares an access modifier, return type, and method signature followed by a semicolon. A non-abstract child class inherits the abstract method and must define a non-abstract method that matches the abstract method.

Can abstract class have constructor PHP?

Like C++ or Java abstract class in PHP can contain constructor also.

What is PHP abstract class?

Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.

Which type of inheritance is not supported by abstract class?

However, there is the advantage of using an interface over an abstract class; that is "Multiple Inheritance Support". In C#, two classes (either abstract or concrete) cannot be inherited by the same derived class. It causes ambiguity in the derived class if both have the same method signature.


1 Answers

Your code sample looks fine in PhpStorm 2017.2 (currently in EAP stage) but shows warning in 2017.1.4.

enter image description here

Quite possibly it was fixed by WI-33991 or one of the related tickets.


You may get & try the 2017.2 EAP build at any time from here: http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Early+Access+Program

It comes with own 30-days license and can run in parallel to your current version (IDE-wide settings are stored in separate folders).

like image 98
LazyOne Avatar answered Oct 11 '22 03:10

LazyOne