Search code examples
phpphpstorm

Is it possible to make PHPStorm include necessary files automatically?


I have a project with lot of files. Normally each file contains one class definition. Currently when I need an instance of object I use loader, which includes necessary file and instantiates it. Such approach though doesn't allow IntelliSense to work properly. And I prefer more readable new MyObject() than $loader->load("MyObject"). I use PHPStorm IDE. Is it possible to configure it to add necessary require_once("some_file.php") when I use appropriate class type?


Solution

  • Solution 1: Switch to using the autoload feature of PHP (5+) and then use new MyObject(): http://www.php.net/manual/en/function.spl-autoload-register.php

    Solution 2: Use a live template defined like this:

    /** @var $CLASSNAME$ $VARNAME$ **/
    $VARNAME$ = $loader->load("$CLASSNAME$");
    

    You can then choose the class name and the vriable name each time you use the livetemplate.