Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Front Controller implementation without singleton : conceptual question

i have a "conceptual" question about Front controller implementation in php.

Most of the Front Controllers i have seen around are implemented with Singleton, i am not a big fan of singleton pattern and i created a Container which has a static property that will store a unic instance of Front Controller.

With singleton, i had to put initialization code inside the constructor (or a method called by the constructor):

$fc = FrontController::getInstance();

With the container i could put configuration outside the FrontController, that was my goal and i still have a simple way to retrieve the FrontController.

$fc = Container->getFrontController();

This code looks much cleaner to me and i can get clean subclassing without caring about parent constructors.

That's quite the same thing at 'bootstrap' time, but in practice the difference from my previous implementation is that now i can create FrontControllers anywhere in the application (inside a DAO or inside a Action), because the constructor is no longer private/protected.

My question is: Is it a 'bad practice' to give to the user of my classes the possibility to create FrontController instances anywhere in the app? I would write documentation and deliver the container with other classes, but i still wonder if i should prevent strange uses.

like image 884
fat Avatar asked Apr 09 '11 14:04

fat


2 Answers

Is there any real reason to create FrontController instances inside the app? If there is then go ahead. Otherwise I am a bit skeptical about this as it may complicate things later on. What I'm afraid of is someone using new FrontController instances when there is a much simpler way, either out of laziness or because they don't know any better. Once they find something that works, some people tend to keep doing it even if there's a better way. Never forget that you may have to work with people that are not as good as you.

Personally I would hide this or not allow it altogether. However I would keep it in mind for later releases. If you ever stumble on a case where it's the best option by all means add it to your "official" interface.

Don't forget that once you release a function into the wild, it's excruciatingly difficult to kill it off.

like image 160
Manos Dilaverakis Avatar answered Nov 16 '22 03:11

Manos Dilaverakis


$fc = Container::getFrontController();

sounds Ok for me.

I don't think it would be a bad practice to allow developers to retrieve the Front Controller instance anywhere, if you control correctly what developers can do with it (e.g. do not overwrite some property or execute its methods in a wrong order without showing an error/warning to the user).

like image 2
Frosty Z Avatar answered Nov 16 '22 03:11

Frosty Z