我正在开发一个 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/