简体   繁体   中英

PhpStorm CodeIgniter class not found

Can you help me on this one. I'm new in CodeIgniter and PhpStorm, I'm having a problem. The PhpStorm IDE is showing an error, even though the code works fine (the Persons (controller) class can't find the Person (model) class).

在此输入图像描述

在此输入图像描述

在此输入图像描述

$data['persons']=$this->**person**->get_person(); = on this syntax from Persons class, there is a message "Field person not found in class Persons ".

Can you enlighten me on how to resolve this, but actually the output is good and it retrieves the data without issue just a warning message in Persons class.

The person property ( $this->property ) is not explicitly declared as it is created and accessed via PHP's magic methods.

PhpStorm has no special support for CodeIgniter framework and cannot guess where $this->person is coming from and what type it is.

But you can help IDE -- just a small PHPDoc comment before the actual class -- using @property tag.

/**
 * @property Person $person Optional description 
 */
class Persons extends CI_Controller {

...and oh magic -- it works: 在此输入图像描述

The answer from LazyOne did not work for me initially. After some testing I found out that my issue was upper/lower case related on how you declare the property in PHPDoc - hopefully the following observations can help someone else. This is what I have to declare my model class:

class Custom extends CI_Model {}

In my controller I load and use the model for example the following way:

$this->load->model('Custom')
$table = $this->Custom->get();

Now for phpStorm to pick up this class correctly I originally added a PHPDoc @property comment above a core class as described by others (either above the CI_Controller class or separate CI_phpStrom.php file) like this:

*
* @property Custom $custom
*

However, this didn't remove the issue, as the variable name becomes case sensitive in this case and I had to write:

*
* @property Custom $Custom
*

for my above controller code to pick up the class correctly. An alternative would be to use lowercase when calling functions (this works even if your model declaration used uppercase)

$this->load->model('custom')
$table = $this->custom->get();

The funny thing all this uppercase or lowercase didn't matter if I call my model class "Custom_model" - then it made no change if set the PHPDoc property variable to $Custom_model or $custom_model ...

Normally most people add the ' _model ' suffix to the Model class names. I suggest that you rename your model with "Person_model" call your model "person_model" like this:

$this->load->model('person_model');
$this->person_model->get_person();

I think that you might find it an adequate solution!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM