douxi8759 2015-01-28 14:50
浏览 82
已采纳

PHPDoc“静态”返回类型在这里表示什么?

I am working on a Symfony project with entities managed by Doctrine. The following is code from my entity:

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 is auto-generated by Doctrine. The getAppointmentsAtDate method was implemented by myself. The method's PHPDoc header was auto-generated by PhpStorm.

What I cannot understand is the static keyword in my custom method's return type.

From my understanding of the PHPDoc types static signifies that this method returns an instance of the class on which it was called, in this case a User instance.

However, I fail to see how this method could ever return a User instance or anything other than an instance of Collection.

So what does the static keyword mean here? Is my understanding of the keyword flawed? Or is PhpStorm's auto-generated documentation header simply wrong?

  • 写回答

3条回答 默认 最新

  • 普通网友 2015-01-28 15:03
    关注

    I have looked at the source of doctrine for the matching function and here is the return type :

    return new static($filtered);
    

    Phpstorm probably parsed the doctrine source and saw the return static statement in the matching function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样