我可以指示返回到PhpStorm的动态类型吗? [英] Can I indicate dynamic type returned to PhpStorm?

查看:412
本文介绍了我可以指示返回到PhpStorm的动态类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个类:

  class Foo 
{
static function test b $ b {
return new static();
}
}

类Bar扩展Foo
{}

类Baz扩展Foo
{}

现在如果调用:

  $ var = Bar :: test(); 

我希望PhpStorm识别 $ var call_class ,其中: Bar



如果我做 $ var = Baz :: test(); $ var / code>实例。



如何获取动态的called_class向PhpStorm指示返回什么类型?



我有一个语法如

  / ** @returncalled_class* / 
解决方案

div>

首先,你的静态函数有一个错误。您不能使用

  return $ this; 

,因为静态调用不会创建任何实例。所以你必须创建一个新的实例。

  class Foo 
{
public static function test
{
return new static();
}
}

static关键字将实例化类的一个新实例

  class Bar extends Foo 
{
public function fooBar(){}
}

class Baz extends Foo
{
public function fooBaz(){}
}

我只是添加了foo函数来显示phpStorm现在可以正确地找到源代码。

 code> $ var = Bar :: test(); 
$ var-> fooBar();

$ var现在是Bar的实例

  $ var2 = Baz :: test(); 
$ var2-> fooBaz();

$ var2现在是Baz的实例


I have 3 classes like :

class Foo
{
    static function test()
    {
       return new static();
    }
}

class Bar extends Foo
{}

class Baz extends Foo
{}

Now if call :

$var = Bar::test();

I want PhpStorm to identify $var as the called_class, here: Bar.

But, if I do $var = Baz::test(); $var is Baz instance.

How can I get the dynamic called_class to indicate to PhpStorm what type is returned?

I there a syntax like

/** @return "called_class" */

to help PhpStorm and indicate the type?

解决方案

First you have an error in your static function. You can not use

 return $this;

as the static call will not create any instance. So you have to create a new instance.

class Foo
{
    public static function test()
    {
        return new static();
    }
}

The static keyword will instantiate a new instance of the class itself.

class Bar extends Foo
{
    public function fooBar(){}
}

class Baz extends Foo
{
    public function fooBaz(){}
}

i just added the foo functions to show you that phpStorm now will correctly find the source.

$var = Bar::test();
$var->fooBar();

$var is now an instance of Bar

$var2 = Baz::test();   
$var2->fooBaz();

$var2 is now an instance of Baz

这篇关于我可以指示返回到PhpStorm的动态类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆