Skip to content
Advertisement

How to indicate in php 7.1 that the return type is the current child type?

I have

JavaScript

$x has no type hinting… I like type hinting, so I want type hinting for B, not for A

How to enable type hinting directly for $x?

what I thought is something like

JavaScript

This obviously doesn’t work

Is there something that does?

Advertisement

Answer

For the example you present, why do you need to have a factory method? You are creating a new instance from the constructor, why don’t you just $x = new B($record)!

UPDATED ABOVE


JavaScript

@return static will type hinting its child class. Also I changed your function to static function, it is typical factory pattern.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement