Interface as a method argument not detected
Hi,
i have an Interface
interface Chicken{};
and class
class MyChicken implements Chicken{};
and method in another class
public function loveChicken(Chicken $chicken){};
and i implement this in my program like:
$chicken = new MyChicken
$this->loveChicken(Chicken $chicken);
well it says the type is not compatible, please chekc
Please sign in to leave a comment.
Hi there,
Please show more realistic code / actual screenshots.
Your code in general works just fine:
I'm not sure if that just a typo you have made or what .. but WTF is this:
Why do you have Chicken before $chicken when executing the code?
it is called polymorphism,
it should be possible because MyChicken is implemented from Chicken
the code also works for me but phpstorm detects as error
>it is called polymorphism,
>it should be possible because MyChicken is implemented from Chicken
And..? It's not the answer for my question. Please check the code fragment again.
In any case -- I gave you the actual code (which was done from your examples) .. and IDE did not complain at all. This means that the difference could be in your real code. Or it could be some corrupted or out-of-sync indexes/caches. Please try "File | Invalidate Caches..." and restart IDE.
I have found that, when using polymorphism without a good code coverage, you will get a highlight, saying the method doesn't exists.
You can fix by:
1. Add "instanceof" conditions
2. Add to the comment above your factory: "@return ObjectParent|ObjectA|ObjectB|ObjectC|ObjectD" - this will allow phpStorm to resolve the function call