Welcome Guest, Not a member yet? Register   Sign In
controller and model with same name

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.

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

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

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

makes it really easy for you to understand at a glance what is going on. You're getting info from your photo model.


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


With an adaptation for CodeIgniter 3 I use this trick:

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

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

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:


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.

Theme © iAndrew 2016 - Forum software by © MyBB