方法@返回属性的子类

发布于 2025-01-20 08:41:30 字数 969 浏览 4 评论 0原文

我有一个抽象类foo和一个摘要建造者foobuilder

abstract class Foo {

}

abstract class FooBuilder {
  protected Foo $model;

  /**
  * Return the class instance
  *
  * @return Model  //What is the correct return type?? 
  */
  public function get()
  {
    return $this->model;
  }
}

我想在我的孩子建造者中使用方法get()返回类型是子类,而不是抽象foo

class Bar extends Foo {
}

abstract class BarBuilder {
  public function __construct()
  {
    $this->model = new Bar();
  }
}

$barBuilder = BarBuilder();
$bar = $barBuilder->get(); //The type is "Bar", but IDE thinks is "Foo"

有什么方法可以返回phpdoc中的静态属性的静态类型?类似@return static($ this-> model)之类的东西?

一个示例是Laravel雄辩在somemodel :: find()中所做的。 IDE知道类型可以是somemodel。但是@return只有模型

I have a abstract class Foo and a abstract builder FooBuilder

abstract class Foo {

}

abstract class FooBuilder {
  protected Foo $model;

  /**
  * Return the class instance
  *
  * @return Model  //What is the correct return type?? 
  */
  public function get()
  {
    return $this->model;
  }
}

I want to use the method get() in my child Builders and the IDE detect that the return type is the child class, not the abstract Foo.

class Bar extends Foo {
}

abstract class BarBuilder {
  public function __construct()
  {
    $this->model = new Bar();
  }
}

$barBuilder = BarBuilder();
$bar = $barBuilder->get(); //The type is "Bar", but IDE thinks is "Foo"

Is there any way of returning a static type of a attribute not the class in PHPDoc? Something like @return static($this->model)?

An example is what Laravel's Eloquent does in SomeModel::find(). The IDE knows that the type can be SomeModel. But the @return only have Model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情哥ㄟ 2025-01-27 08:41:31

在DocBlock中使用仿制药确实有助于phpstorm(我的是2021.2.2):

class Foo
{
}

/**
 * @template T of Foo
 */
class FooBuilder
{
    /**
     * @var T
     */
    protected Foo $model;

    /**
     * @return T
     */
    public function get(): Foo
    {
        return $this->model;
    }
}

class Child extends Foo
{
    public function testCompletion(){}
}

/**
 * @extends FooBuilder<Child>
 */
class ChildBuilder extends FooBuilder
{
}

class AnotherChild extends Foo
{
    public function otherMethod(){}
}

/**
 * @extends FooBuilder<AnotherChild>
 */
class AnotherChildBuilder extends FooBuilder
{
}

$childBuilder = new ChildBuilder();
$childBuilder->get()->testCompletion(); // testCompletion is suggested

$anotherChildBuilder = new AnotherChildBuilder();
$anotherChildBuilder->get()->otherMethod(); // otherMethod is suggested

Using generics in docblock does help PhpStorm(mine is 2021.2.2):

class Foo
{
}

/**
 * @template T of Foo
 */
class FooBuilder
{
    /**
     * @var T
     */
    protected Foo $model;

    /**
     * @return T
     */
    public function get(): Foo
    {
        return $this->model;
    }
}

class Child extends Foo
{
    public function testCompletion(){}
}

/**
 * @extends FooBuilder<Child>
 */
class ChildBuilder extends FooBuilder
{
}

class AnotherChild extends Foo
{
    public function otherMethod(){}
}

/**
 * @extends FooBuilder<AnotherChild>
 */
class AnotherChildBuilder extends FooBuilder
{
}

$childBuilder = new ChildBuilder();
$childBuilder->get()->testCompletion(); // testCompletion is suggested

$anotherChildBuilder = new AnotherChildBuilder();
$anotherChildBuilder->get()->otherMethod(); // otherMethod is suggested
撑一把青伞 2025-01-27 08:41:31

您可能应该将FOO用作示例中的返回类型;但是为了娱乐,您可以使用静态返回类型来确定下面的子实例

class Foo
{
    /**
     * @return string
     */
    public function Iam(): string
    {
        return "hello";
    }
}

class Helper
{
    /**
     * Return the class instance
     *
     * @return static
     */
    public static function get(): static
    {
        return new self();
    }
}

class FooBuilder extends helper
{
    protected Foo $model;

    public function mememe()
    {
        echo "I am a method";
    }
}

class FooBuilder2 extends helper
{
    protected Foo $model;

    public function xray()
    {
        echo "I am a method";
    }
}

$test = FooBuilder::get();
$test->mememe();

$test2 = FooBuilder2::get();
$test2->xray();

You should probably use Foo as the return type in your example; but for fun, you can use static return type to determine child instance like below

class Foo
{
    /**
     * @return string
     */
    public function Iam(): string
    {
        return "hello";
    }
}

class Helper
{
    /**
     * Return the class instance
     *
     * @return static
     */
    public static function get(): static
    {
        return new self();
    }
}

class FooBuilder extends helper
{
    protected Foo $model;

    public function mememe()
    {
        echo "I am a method";
    }
}

class FooBuilder2 extends helper
{
    protected Foo $model;

    public function xray()
    {
        echo "I am a method";
    }
}

$test = FooBuilder::get();
$test->mememe();

$test2 = FooBuilder2::get();
$test2->xray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文