This article introduces you to the correct annotation @return to let PHPstorm dynamically return the class. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The scenario is like this. There is a BaseModel (inherited from ActionRecord), all other models inherit from it, and there is a method in it. Simply paste the code of this class:
class BaseModel extends ActiveRecord { protected $temp_model; public function getCacheModel() { return $this->temp_model; } }
The function of this method is to obtain the cached instance object found from the database during parameter verification.
At this time, the problem came. When I took out this object, PHPstorm had no prompts (such as method prompts, attribute prompts, etc.). According to the general situation, I only need to add the @return annotation in front of the method. .
/** * @return static */ public function getCacheModel() { return $this->temp_model; }
Let’s continue to study in depth. Regarding the meaning of static, I specifically checked it on PHPDoc.
static
An object of the class where this value was consumed, if
inherited it will represent the child class. (see late static binding in
the PHP manual).
Google translated it, the general idea is as follows:
The object of the class that consumes this value, if it is inherited it will represent a subclass.
(See Late Static Binding in the PHP Manual).
The general meaning is that the class that called this method will be returned. If the method is called by a subclass of the parent class, the subclass will be returned.
There are 2 similar ones
self
An object of the class where this type was used, if inherited it will still represent the class where it was originally defined.
$this
This exact object instance, usually used to denote a fluent interface. its class.
$this: This exact object instance is usually used to represent a fluent interface.
is similar to self.
But at this point, my problem is still not solved. No matter what I change the value of @return to, BaseModel is still returned, even though I print self::className() in this getCacheModel() method. , what appears is the subclass name.
public function actionCommitReward() { $model=$this->goCheck(new TakeRewards(['scenario'=>'commit_reward'])); //获取实际要修改的数据 $reward = $model->getCacheModel(); }
$model It is obtained by calling $this->goCheck()
. Let’s take a look at the goCheck method://验证参数是否合法 public function goCheck($model, $dada = '') { $data = $this->postData;//post传入的数据 if ($model->load($data, '') && $model->validate())//数据效验
else (new PublicFunction())->returnWayTip('1001', PublicFunction::getModelError($model));//这里理解成抛异常 }
/** * @param object $model * @param string $dada * @return model1|model2 */
This time I really realized the importance of comments. . . It turns out that the reason why PHPstorm prompts is because everyone wrote comments according to PHPDoc specifications!
Finally, some students may ask, why not put the goChekc method in BaseModel? Yes, in fact the standard approach should be like this, but because I assigned Yii::$app->request->post() to $this->postData in the controller (although this is convenient) Diudiu), and some operations of changing tokens to IDs were manually assigned, so there is no way because the postData cannot be obtained in the model. Of course, you must move it in, but you need to pass parameters every time. $this->postData, it’s a matter of opinion.
php recursive function return may fail to return the desired value correctly
In PHP Methods that return reference types, php returns reference types
The above is the detailed content of How to correctly annotate @return to let PHPstorm dynamically return classes. For more information, please follow other related articles on the PHP Chinese website!