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

5 comments
Comment actions Permalink

Hi there,

Please show more realistic code / actual screenshots.

Your code in general works just fine:

interface Chicken
{
}

class MyChicken implements Chicken
{
}

class SomeClass
{
public function loveChicken(Chicken $chicken)
{
}
}

$someClass = new SomeClass();
$chicken = new MyChicken();
$someClass->loveChicken($chicken);

I'm not sure if that just a typo you have made or what .. but WTF is this:

$this->loveChicken(Chicken $chicken);

Why do you have Chicken before $chicken when executing the code?

1
Comment actions Permalink

it is called polymorphism,

it should be possible because MyChicken is implemented from Chicken

 

 

0
Comment actions Permalink

the code also works for me but phpstorm detects as error

0
Comment actions Permalink

>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.

1
Comment actions Permalink

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

 

0

Please sign in to leave a comment.