How to declare return type of inherited method?

How to declare return type of inherited method? I want to avoid having to declare /** @var **/ all the time when using some classes!

As a sample:


interface CollectionInterface
{
    public function get($key);
}

interface UserCollectionInterface extends CollectionInterface
{
// How to declare the return type of this method that is inherited?
//     public function get($key);    
}


Thanks!
7 comments
Comment actions Permalink

but the method is declared on a parent!

0
Comment actions Permalink

I see 2 interfaces (with the same method). I assume that you can add proper PHPDoc for any of them. Is that correct?

In any case: please provide some more realistic example stating which classes/interfaces you can edit and which are not. Because right now I do not understand/see no reason why you cannot use @return tag here.

0
Comment actions Permalink

ok, see this example

interface CollectionInterface
{
    public function get($key);
}

interface UserCollectionInterface extends CollectionInterface
{
//     here get returns an instance of User


}

interface CarCollectionInterface extends CollectionInterface
{
//     here get returns an instance of Car
}

interface LampCollectionInterface extends CollectionInterface
{
//     here get returns an instance of Lamp
}
0
Comment actions Permalink

And pretty much would need to know how to declare the return of a method without redeclaring the parent method.

interface a
{
    public function get();
}

interface b extends a
{
// here b should return instance of class X. How to declare that without redeclaring the method or altering the parent interface
}

0
Comment actions Permalink

Well... the only thing that could work is @method in PHPDoc for the class

/**
* @method ClassX get()
*/
interface b extends a


I'm just still cannot confirm for sure that it will work in PhpStorm since I still have not seen fully working example from you (I mean: so far you are showing interfaces only and not a single class and how they (interfaces) will be actually used).

You will have to implement that method at some point .. and maybe it will override everything...

0
Comment actions Permalink

It does work!

interface a
{
    /**
     * @return StdClass
     */
    public function get();
}

/**
* @method UserClass get()
*/
interface b extends a
{
}

$test = "";
/** @var b $test */
$test->get()->getName(); // Autocompletes getName from UserClass




Thanks!!!

0

Please sign in to leave a comment.