Chris Chris - 7 months ago 14
PHP Question

What does PHPDoc "static" return type signify here?

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?

Answer

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.