Pre-defining Class Variables in PHP (Yii Models)

I was coding in PHP, using the Yii framework. The columns in Yii turn into Model variables. These variables are automatically created by Yii so I don't think I should need to create them. I do not get any errors or warnings on the site when I run the app without the variables pre defined. I have my framework on PHPStorm set to Yii so I was wondering if someone could get me some answers. I apologize if this question has been asked already.

Here is an example. Lets say I have a table in my database.

id  | type  | width | height |
1  | 'input' | 300   | 200    |

And a Model representing that table.

class Form extends CActiveRecord {

In my controller I am going to ask for those variables in the database based off of ID.

class FormController extend CController {
  public function actionIndex() {
    $form_model = new Form;
    $form = $form->findByPK(1);
    $form->id; // PHPStorm warning
    $form->type; // PHPStorm warning
    $form->width; // PHPStorm warning
    $form->height; // PHPStorm warning

In PHPStorm I get warnings as defined in the comments. The warning is that the variables are not defined. So when I go back to my model and add them to the global scope.

class Form extends CActiveRecord {
  public $id;
  public $type;
  public $width;
  public $height;

The warnings fade. I feel like those variables are already created by Yii. but PHPStorm doesn't know that. I am also connected directly to my database so PHPStorm should know that the models and tables all match up. If someone could tell me why this behavior is occurring. Thanks and sorry again if this has already been answered.


Hi Dillon,

You need to use @property PHPDoc declaration for your class:


So I do that for each variable? I think I will request that to be automatically added, I think it wouldn't be too hard to add the functionality. Or the error messages should suggest that to add a magic property in a phpdoc.


So I do that for each variable? I think I will request that to be automatically added,

Good luck with waiting. You see -- Yii is not used by 99% of PHP developers, there are plenty other popular frameworks around -- every each of them wants to be "that special" that is supported by that specific / any IDE. Unfortunately, PhpStorm is not at that level yet to concentrate developers resources on support for specific framework only -- much better if there will be something that will suit any framework / most frameworks. And magic functionality requires IDE to know about how that specific framework works; IDE needs to provide some quite tight integration in case...

I think it wouldn't be too hard to add the functionality.

I think the same -- it would not be too hard to add field description manually (actually, half manually, as IDE can offer a hand here with creating such fields / @property entries -- just try Alt+Enter on problematic place).

Or the error messages should suggest that to add a magic property in a phpdoc.

Alt+Enter (Quick Fix intentions) may help here.

Alternative approach is to disable (or lower severity) corresponding inspection (Settings | Inspections | PHP | Undefined | Undefined field) -- no warning messages, no worries -- right? Of course -- it's a radical measure which should not be really used (but for some people it was an accetable solution).


Thanks yeah, I will take everything you said into heart. I would like my code to be at it's best so thats why I won't disable the warnings, I think its good that it catches undefined variables, but now that I know about the magic properties I will have to go in and edit all 37 models manually... tick tock

Thanks again.


Unfortunately it is the current state of things -- you cannot add such information into existing project quickly (I mean -- in few clicks only).

Based on status of current popular tickets that have quite a few votes ... I do not expect to see that deep Yii (or any other) framework  integration in upcoming version. There should be some support for Symfony/Yii (accordingly to their roadmap) but nothing major: Some of those tickets have "In Progress" status for quite a while or got scheduled from version to version .. but no great progress has been made (that's why I said "Good luck with waiting").

No doubts -- support for popular framework is always a good thing -- it makes a lot of people happier and attracts new users -- you do not have to think about soma basic things (as it managed behind the scene somehow) .. and some complex things are made simple (like the one in your case). Unfortunately such support is not currently here yet .. and therefore it needs to be done in more "traditional" / framework-independent way (PHPDoc tags in this case).

Without such deep integration (when IDE knows how framework works)  .. I do not see what you can do here to have such fields be recognized by IDE quickly. One thought though -- you can speedup such task quite a bit by making some custom script that will generate such PHPDoc instructions which you can later just copy-paste. If you write (or find existing one) something like this now (spend a day .. or maybe just a few hours), you can later reuse it in another project. It's definitely better that doing it manually ... or not doing it at all.


In case anyone comes by here wondering about Yii2 integration with PHPstorm, the PHPdoc required for the model variables is provided by Gii when it creates models automatically.  I'm currently trying to work out how to get the same service for view variables which are passed to the view via $controller->render and ->renderPartial.


Please sign in to leave a comment.