Home » Php » php – IntelliJ IDEA 12 not finding CodeIgniter classes, throwing errors

php – IntelliJ IDEA 12 not finding CodeIgniter classes, throwing errors

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m using IntelliJ IDEA 12 to develop a PHP web application using the CodeIgniter framework. Due to the way CI instantiates objects and methods, IDEA thinks they don’t exist when they’re called:

enter image description here

As you can see, I get those annoying orange squigglies under every single CodeIgniter class/method I try to use.

Here’s the code for the CI_Controller class, giving some insight into how they’re creating the objects:

class CI_Controller {

    private static $instance;

    /**
     * Constructor
     */
    public function __construct()
    {
        self::$instance =& $this;

        // Assign all the class objects that were instantiated by the
        // bootstrap file (CodeIgniter.php) to local class variables
        // so that CI can run as one big super object.
        foreach (is_loaded() as $var => $class)
        {
            $this->$var =& load_class($class);
        }

        $this->load =& load_class('Loader', 'core');

        $this->load->initialize();

        log_message('debug', "Controller Class Initialized");
    }

    public static function &get_instance()
    {
        return self::$instance;
    }
}

Is there any way I can get IntelliJ IDEA to either ignore these errors (but not other errors), or get it to find the objects on its own?

Note: I used to use PHPStorm, a PHP IDE build on the IntelliJ IDEA platform, and it did the same thing when it came to CodeIgniter.

How to&Answers:

I solved this by following this guide. It was meant for PHPStorm but works for IntelliJ IDEA as well (probably WebStorm too!). It was very simple, actually. A basic recap:

  1. Saved the following PHP code in my project root. It’s basically just a PHPDoc comment:

2. Found /system/core/controller.php and /system/core/model.php inside of IntelliJ IDEA and right clicked->Mark as Plain Text.

3. Done! It works great, no more false errors, and code completion!

enter image description here

Answer:

This is the solution that worked for me: https://github.com/Stunt/Codeigniter-autocomplete/

I’m using phpStorm but I imagine it would work with IDEA.

No need to mark files as plain text. Just drop the autocomplete.php file into the application/config folder and it works. I doubt the link at github will go bad but just in case here is the content of the file.

UPDATE for CIv3: If you are using CodeIgniter V 3.x, use this instead.

I cannot take credit for all of this. I found a lot of these but do not recall where. I did need to add some to make it complete though.