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

Source: Internet
Author: User
Tags yii
This article introduces to you about the correct annotation @return let Phpstorm dynamic return class, there is a certain reference value, the need for friends can refer to, I hope to help you.

The scene is this, there is a Basemodel (inherited from Actionrecord), all the other model inherits from it, and then there is a way to 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 an instance object that is isolated from the database when doing parameter validation.
This time, the problem came, I take out this object, Phpstorm without hints (such as method hints, property hints, etc.), according to the general situation, only need to precede the method with @return comments on it.

/** * @return static */public function Getcachemodel () {    return $this->temp_model;}

We continue to delve into the meaning of this static, I have specifically consulted on the Phpdoc,

Static
An object of the class where this value is consumed, if inherited it would represent the child class. (see late static binding in the PHP manual).

Google Translate, the effect is as follows:
The object that consumes this value for the class, and if it inherits it will represent the subclass.
(see later static bindings in the PHP manual).

A class that calls this method is probably meant to be returned, and if it is called by a parent-class method subclass, the subclass is returned.

There are 2 similar

Self
An object of the class where this type is used, if inherited it would still represent the class where it was originally de Fined.
$this
This is exact object instance, usually used to denote a fluent interface.

The literal translation is as follows
Self: An object that uses this type of class, and if it inherits it, it will still represent the class that originally defined it.
The effect is similar to static, but the parent class method child class is called and still returns the parent class.
$this: This exact object instance is typically used to represent a smooth interface.
Almost like self.

But here, my question is still unresolved, no matter what the value of my @return change, still return is Basemodel, although I am in this Getcachemodel () method to print Self::classname (), the name of the subclass appears.

So we continue to look up, I was called by the Controller, the controller's code is as follows:

Public Function Actioncommitreward () {    $model = $this->gocheck (new Takerewards ([' scenario ' = = ' Commit_ Reward ']));    Gets the data that is actually being modified    $reward = $model->getcachemodel ();}

There seems to be no problem, this time we have to notice, $model is called by $this->gocheck () , we go to see the Gocheck method:

Verify that the parameter is valid public function Gocheck ($model, $dada = ') {    $data = $this->postdata;//post Incoming data    if ($model- Load ($data, ') && $model->validate ())//data validation

return $model;

    Else (new Publicfunction ())->returnwaytip (' 1001 ', Publicfunction::getmodelerror ($model));//This is understood as throwing exceptions}

There is no specification here, because the model (object type) is passed in here, so phpstorm does not know what class we are specifically passing in, plus the comment:

/** * @param Object $model * @param string $dada * @return Model1|model2 */

After this, the problem "barely solved". Just by adding a table, you will need to add the class name corresponding to the table in the @return, and there will be a prompt for properties that the class should not have existed.

Why can't we use static here? Since this is called by $this, returning the controller class, and this also causes the later use of the $model->getcachemodel () method, There is no way to properly identify the class that should be returned (the return class depends on what the Gocheck @return Comment is).

Of course you can not write the comments, then you will find, so the hints are gone.
This time I really realized the importance of annotations ... The original phpstorm reason are prompted, are because everyone according to PHPDOC specifications written comments Ah!

Finally, perhaps the reunion asked, why not put the Gochekc method in the Basemodel? Yes, actually the norm is supposed to be like this, but because I put yii: $app->request->post () in the $this->postdata of the controller (though it's easy to lose), And in doing tokens for the ID of some operation has been manually assigned, so there is no way, because the model can not get this postdata, of course, you must move in is also possible, but each time need to pass the $this->postdata, a matter of opinion.

However, these 2 methods are not standardized, $this->postdata = Yii:: $app->request->post (); To change the global variable to a local variable, the canonical practice should be to use Yii:: $app->request->post ($name, $dafaultValue) to assign the post data.

Finally, because I am not a person to write, so there is no way to make drastic changes, only as far as possible to optimize.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.