Can you give me an example? My opinion isnt that you should have an agrument just for a pass through like you discribe, but cant see many situations where this is a case. Maybe more than one config objects is the right thing for you and just creat it when its needed? Or maybe create the object that needs the config outside the "first" class and inject it?
Originally Posted by Derokorian
One of the main goals in zend framework 2 is to get rid of all the singelton use they have, because its considered as bad practice and a pain to test in bigger applications.
Sure how about MVC? Everything runs from the controller right? But why would the controller need to know the DB configuration? So the process would be God controller gets called, it has to be instantiated with the config to put it in the right scope. God then calls any number of other controllers who in turn instantiate their respective models, these controllers also need to have the config passed to them, just so they can pass it to the models. OR I can just have the model request the configuration item it needs from my configuration registry and neither of the controllers need to care that there is even a configuration involved in the model.
I'd also like to point out that everything I know about PHP comes from the manual and these forums. I've never gone to school for anything related to computers and everything I know about them is self taught. So my assumptions may not be considered best practice but until someone can logically show me why my assumptions are bad I will continue to base my decisions off them.
Last edited by Derokorian; 04-17-2012 at 01:09 PM.
Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
I'd rather be a comma, then a full stop.
User Authentication in PHP with MySQLi
- Don't forget to mark threads resolved - MySQL(i) warning
I have a simple MVC my self and can show an example how I do it. I dont say its the best way, but its no defined constants and no singletons there.
pseudocode for Application::__construct($environment)
Then in the dispatcher I find the right controller and creates a controller object that takes $request and $config as parameters in the constructor.
// Do some initial stuff and...
$autoloader = new Autoloader();
$config = new Config($filename);
$errorhandler = new Errorhandler($config);
$request = new Request();
// Do some routing.
$router = new Router($config, $request);
$dispatcher = new Dispatcher($config, $request);
I have a base controller class for the spesific application that all the other controllers inherits from and can load the database there if its used all over. I also creates the models, main layout that is used all over. If you want to create the database here and pass it to the model or just send the config to the model and let it do its own stuff should be nearly the same. If you dont use database in all your controllers, create it in the controller constructor that dos.
With my naming conventions and autoloader there is no hard coding of anything(outside the the bootstrap), no globals and no singletons.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)