Home >Backend Development >PHP Tutorial >How to correctly annotate @return to let PHPstorm dynamically return classes

How to correctly annotate @return to let PHPstorm dynamically return classes

不言
不言Original
2018-08-01 11:33:583932browse

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.

The general idea is that it is similar to static, but when the parent class method is called by a subclass, it still returns the parent 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.

So we continue to look above. I called it in the controller. The controller code is as follows:

public function actionCommitReward()
{
    $model=$this->goCheck(new TakeRewards(['scenario'=>'commit_reward']));

    //获取实际要修改的数据
    $reward = $model->getCacheModel();
}
It seems that there is no problem. We should pay attention to it at this time.

$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())//数据效验
return $model;
    else (new PublicFunction())->returnWayTip('1001', PublicFunction::getModelError($model));//这里理解成抛异常
}
Irregularities appear here, Since the model (object type) is passed in here, PHPstorm has no way of knowing what class we are passing in. After adding the comment:

/**
 * @param object $model
 * @param string $dada
 * @return model1|model2
 */
, the problem is "barely solved". It's just that every time a table is added, the class name corresponding to the table will need to be added to @return, and attributes of the class that should not exist will be prompted.

Why can't static be used here? Because $this is called here, returning the controller class is of no use, and this also leads to the fact that when the $model->getCacheModel() method is used later, there is no way to properly identify the class that should be returned (what class is returned depends on goCheck what the @return annotation is).

Of course you don’t have to write comments, then you will find that all the prompts are gone.

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.

However, these two methods are not standardized. $this->postData = Yii::$app->request->post(); turns the global variable into a local variable. , the standard approach should be to use Yii::$app->request->post($name,$dafaultValue) to assign values ​​to post data.

Finally, because I am not writing it alone, there is no way to make drastic changes, I can only optimize it as much as possible.

Recommended related articles:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn