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:
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.
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.
Like C++ or Java abstract class in PHP can contain constructor also.
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.
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.
Your code sample looks fine in PhpStorm 2017.2 (currently in EAP stage) but shows warning in 2017.1.4.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With