What does the β€œstatic” type of PHPDoc mean here?

I am working on a Symfony project with objects managed by Doctrine. Below is the code from my object:

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 automatically generated by Doctrine. The getAppointmentsAtDate method was implemented by me. The PHPDoc header method was automatically generated by PhpStorm.

What I cannot understand is the static in my return type.

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

However, I do not see how this method can ever return a User instance or anything other than a Collection instance.

So what does the static keyword mean here? Is my understanding of the keyword spoiled? Or just the title of a phpStorm auto-generated document is simply wrong?

+6
source share
3 answers

I reviewed the source of the doctrine for the matching function and here is the return type:

 return new static($filtered); 

Perhaps Phpstorm analyzed the source of the doctrine and saw the returned static instruction in the corresponding function.

+7
source

Correct understanding of the static keyword.

Your code:

 return $allAppointments ->matching($criteria); 

Returns the results of the matching function from the Doctrine\Common\Collections\ArrayCollection , which returns:

 return new static($filtered); 

As you can see on line 385 of the ArrayCollection.php file

Most likely where PHPStorm displays a possible return type. Code:

 new static() 

It seems PHPStorm will return a new instance of the static class. This is probably not a correct interpretation, but you should see how an automatic system does not necessarily know the difference between:

 new someclass(); // and new static(); 
+2
source

Your understanding of a static keyword sounds correct. The PHPDoc of the matching method reports what Collection returns. It seems to me that PhpStorm made a mistake. Perhaps you made some changes to the code after you created it?

0
source

Source: https://habr.com/ru/post/981676/


All Articles