Search code examples
codeigniterphpstormcodeigniter-3php-ide

How to let the editor know about dynamic properties?


The properties are loaded dynamically in CodeIgniter.

How to let the editor know it should be allowed?

enter image description here

enter image description here


Solution

  • If you know the full list of dynamic properties you're going to use, you can add them as @property docblock annotations on the class, in the format @property type name description.

    The classic use case for this is where the class implements __get to lazy-load data, or in your case dependencies injected by the framework.

    /**
     * @property FormValidation form_validation Form validation object injected by CodeIgniter
     */
    class Example {
        public function foo() {
            $this->form_validation->set_rules('blah', 'blah', 'blah');
        }
    }
    

    Note that these magic properties are assumed to be inherited, and public, so the following will not show warnings:

    // Using magic property in child class
    class ExampleChild extends Example {
        public function somethingElse() {
            $this->form_validation->set_rules('something', 'else', 'entirely');
        }
    }
    
    // Using magic property from outside class
    $foo = new Foo;
    $fv = $foo->form_validation;