CodeIgniter Forums
controller and model with same name - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: controller and model with same name (/showthread.php?tid=25483)



controller and model with same name - El Forum - 12-13-2009

[eluser]bhbutter123[/eluser]
I have a controller and model with the same class name (photos) and when I try to use the photos model in the photos controller I get a cannot redeclare class photos error(as I should). Is there a way I can have a model and controller with the same name because it would get annoying to have to keep typing out _model whenever I include the model.


controller and model with same name - El Forum - 12-13-2009

[eluser]richthegeek[/eluser]
No - this isn't an issue with CI but with PHP itself - you cannot have two classes instantiated with the same name.


controller and model with same name - El Forum - 12-13-2009

[eluser]Ben Edmunds[/eluser]
It's a namespace issue. The short answer is what richthegeek said, they need to be named differently.


controller and model with same name - El Forum - 12-14-2009

[eluser]CroNiX[/eluser]
I name all my models: controllerName_model. It avoids your problem and I think it makes for more semantic code.

Like:
$this->photo_model->get_all();
makes it really easy for you to understand at a glance what is going on. You're getting info from your photo model.


controller and model with same name - El Forum - 08-01-2014

[eluser]jgaujardtj[/eluser]
Hello,

What do you think about :
- named all controller as plural (eg: Users)
- named all model as singular (eg: User)

Joël


controller and model with same name - El Forum - 08-01-2014

[eluser]ivantcholakov[/eluser]
With an adaptation for CodeIgniter 3 I use this trick:

Hack 2. Prevent Model-Controller Name Collision, http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/


controller and model with same name - El Forum - 08-01-2014

[eluser]jgaujardtj[/eluser]
Thanks for your reply ivantcholakov,

So you prefer to modify the core and suffix controller instead of model.
Sorry I'm not a fan of this solution.
Thanks anyway


controller and model with same name - El Forum - 08-01-2014

[eluser]ivantcholakov[/eluser]
I've modified the hack for maintaining backward compatibility, so for me '_controller' suffix is not mandatory. But I prefer for new projects always to use the '_controller' suffix (for controllers) for newly created code. As the article mentioned you will use model names much often, so it is preferable model names to be cleaner and expressive. I hate seeing things like:

$this->users_model->...
$this->users_m->...

They uglify code in unacceptable for me level.

I think, the solution is nice, it merits to be implemented (with adaptation and BC) within CodeIgniter 3.

Of course, for your particular case it is your project and your own decision.