php - PHPDoc "static"返回类型在这里表示什么?

标签 php symfony doctrine-orm phpstorm phpdoc

我正在开发一个 Symfony 项目,其中的实体由 Doctrine 管理。以下是我实体的代码:

class User {
    /**
     * @ORM\OneToMany(targetEntity="Appointment", mappedBy="user")
     */
    private $appointments;

    /**
     * Get appointments
     *
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function getAppointments()
    {
        return $this->appointments;
    }

    /**
     * Get appointments at a specified date
     *
     * @param \DateTime $date
     * @return \Doctrine\Common\Collections\Collection|static
     */
    public function getAppointmentsAtDate(\DateTime $date) {
        $allAppointments = $this->getAppointments();

        $criteria = Criteria::create()->where(/* some clever filtering logic goes here */);

        return $allAppointments ->matching($criteria);
    }
}

getAppointments 由 Doctrine 自动生成。 getAppointmentsAtDate 方法是我自己实现的。该方法的 PHPDoc header 由 PhpStorm 自动生成。

我无法理解的是自定义方法返回类型中的 static 关键字。

根据我对 PHPDoc types 的理解static 表示此方法返回调用它的类的实例,在本例中为 User 实例。

但是,我看不出这个方法怎么会返回一个 User 实例或除 Collection 实例之外的任何东西。

那么这里的static关键字是什么意思呢?我对关键字的理解有缺陷吗?还是 PhpStorm 自动生成的文档 header 完全错误?

最佳答案

我查看了 matching function 的学说来源这是返回类型:

return new static($filtered);

Phpstorm大概解析了doctrine源码,看到了匹配函数中的return static语句。

关于php - PHPDoc "static"返回类型在这里表示什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195214/

相关文章:

doctrine-orm - Doctrine 在运行时交换表

php - 返回后使用 break

symfony - 使用 Symfony2 为 Doctrine 的 PersistentCollection 指定索引

php - Doctrine2 实体管理器在命名空间中找不到自定义存储库类

symfony - Gedmo Doctrine Extensions 中的个人翻译是什么?

php - 如何在Sonata Admin的Show Action中显示图像?

php - 如何使用 doctrine2 创建数据库?

javascript - 在函数调用的参数中分配变量名

java - 如何通过已经运行的 Java 运行命令?

php - php 中的简单 openID