Intellisense on dynamically loaded classes

I have searched discussions and google trying to see if I am missing something. I found one other person using fairly close code design to mine. Basically i am working with something resembling a dynamically loaded factory design.

/** * @static * @param $module * @param $controller_name * @param bool $data */ public static function load_admin_module($module, $controller_name, $data = false) {      global $p;      $p->load($module);      // Load the module script      if(!require(ROOT_PATH . PROJECT_PATH `     . 'library/modules/'.$module.'/admin/controller/control.'. $controller_name .'.php')) {           die('Could not load page controller.');      }      // Initiate the module class
     $class = $module.'_'.$controller_name;      $obj = new $class($data);      $obj->display_message = display_message($obj->display_message);      // Return the module object      return $obj; }

$p->load loads any dependencies that the "controller" might require. When i use this code intellisense gives no results. Here is an example of it being used:

$page = PageController::load_admin_module($module_config->package, 'view');


$page = PageController::load_admin_module('entry', 'view');

The only way i can get intellisense to work is by doing:

/** @var $page entry_view */ $page = PageController::load_admin_module($module_config->package, 'view');

Which defeats quite a bit of purpose of my controller. The idea is to prevent me form including all files one at a time, and finding the path to class files, as well as the name of a class file.

Does anybody have any insight as to how to market his work? Apparently in PHPStorm V1 you could use variables in the phpdoc @return declaration, but it was removed in Version 2.

Any help would be very much so appreciated!
Comment actions Permalink

Hi Marin,

/** @var $page entry_view */$page = PageController::load_admin_module($module_config->package, 'view');

Currently this is the only way of telling PhpStorm what that $page variable is (and for me it is the most correct way).

There is no way PhpStorm to detect what the final class would be unless some helper "translation" metadata can be somehow provided (still in the works ---- please watch/vote/comment/etc this ticket: ). But even then -- how PhpStorm can know what $module_config->package value is (which can be changed dynamically at runtime -- e.g. value loaded from DB)? Surrely,  ('entry', 'view') is much better as you have plain text values which can be parsed and used in such "translator" during static code analysis.

Comment actions Permalink

Thank you for the response,

Is there somethign i can change in my controller in the way i am loading my modules for phpstorm to understand what object i am working with? I was under the impression that PHPStorm fully support factory method intellisense, if i were to switch to a different method of input for my load_admin_module method is there a way i can do a @return $class_name ? Because every scenario i have tried seems to fail currently. And it feels quite redundant typing the variable at the top. I may as well just type the controller code out on the page instead of using the controller its self.

Comment actions Permalink

No, because type of return variable will be different all the time (depends on actual parameters).

Either wait for that ticket to be implemented or keep using PHPDoc comment (personal opinion: which is absolutely fine in my eyes (little explanation/documetation of "behind-the-scene" magic) -- it will also help reading the code for another person who may use this code later).

If you are using some module(s) more (much more) frequently than others, then you may consider creating separate method just to load such module -- there you can use @return tag with correct return type (will remove the need for inline PHPDoc comments in such moments).

If you know Java and have time you can code such plugin yourself -- like this guy did:

Comment actions Permalink

So there is no factory method support for auto detecting the types currently in phpstorm? if not i guess i might have to take a weekend crash course on java and build a plugin. :)

Comment actions Permalink

If it would be supported, do you really believe that this ticket (in such general form) would still exist? Did you even bother to check that ticket, to check what it is about and what is the current state?

Of course, if your modules/controllers (or whatever) have common parent, then by adding proper @return tag (which you are missing btw) to your load_admin_module function (e.g. @return ParentController) would solve some issues (at least it will know the methods that available to that common ParentController class), but it still will not provide methods that are specific for that specific controller implementations (e.g. MySpecialController). Other than that -- see previous messages.

Comment actions Permalink

I have seen that ticket but they are discussing the outline being in an XML file.

And on the comment of the return, i removed it before posting as i had been trying a number of things (such as the link posed above using variables in the return name in whihc no one ever responded to his post). The whole issue is i don't know the object type, as the object type is stored in a variable.


Please sign in to leave a comment.