Configuration Registry - Page 2
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18

Thread: Configuration Registry

  1. #16
    Senior Member
    Join Date
    Aug 2002
    Location
    Norway
    Posts
    160
    Quote Originally Posted by Derokorian View Post
    ... This creates a chained effect, if I want to use a class that needs this argument from inside another class that DOESN'T need this argument, I then I have to pass it to the class that doesn't need it, just so the class it uses for a function can have it...
    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?

    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.
    have a nice OO day!

  2. #17
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,789
    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 02: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

  3. #18
    Senior Member
    Join Date
    Aug 2002
    Location
    Norway
    Posts
    160
    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.

    index.php
    PHP Code:
    <?php
    require __DIR__.'/wwf/Application.php';
    new 
    Application('development');
    pseudocode for Application::__construct($environment)
    PHP Code:
       // Do some initial stuff and...

       
    require __DIR__.'/Autoloader.php';
       
    $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);
       
    $dispatcher->run(); 
    Then in the dispatcher I find the right controller and creates a controller object that takes $request and $config as parameters in the constructor.

    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.
    have a nice OO day!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •