SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 41

Thread: Configuration

  1. #1
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Configuration

    Hi,

    I know there has been this type of thread before, but not for a while, and it never really came to a conclusion. So it's my turn to post this question.

    Here's what I'm planning to do. Feel free to tell me I'm utterly wrong.

    I have only one constant, APPLICATION_PATH, simply defined as:

    PHP Code:
    define('APPLICATION_PATH'dirname(__FILE__)); 
    There don't need to be many includes anyway, because I have an autoload implementation.

    Anyway, besides this, the rest of the configuration will be in an XML file. It'll have action mappings and other general metadata.

    I want to have a Configuration class that allows for fetching this data, probably via XPath queries (most flexible way). However, I don't want to make it a singleton, because it'd be too hard to unit test that.

    What is the best way to go about this? I could pass it around, but that'd make an extra parameter to a lot of constructors.

  2. #2
    SitePoint Member
    Join Date
    Apr 2006
    Location
    Stockholm, Sweden
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about using a registry? Works for me, and it's not that much hassle.

  3. #3
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean a singleton registry, or one that is passed around?

    either way, then the dependencies aren't that clear so you basically have the same problem as singletons. And a singleton registry doesn't help the issue at all.

  4. #4
    SitePoint Member
    Join Date
    Dec 2005
    Location
    Moscow
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Having to manipulate filename strings in random places is ugly. Globals constants like APPLICATION_PATH is no way to OOP.

    Registry interface can be implemented in many ways. Singletons, dubletons does not matter to the application design.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a general solution, I say pass it around. For this particular issue though, you should be able to use the include_path.

  6. #6
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Omut, what is wrong with a constant like APPLICATION_PATH, and what's it have to do with a proper OO implementation?

    I understand there are different ways of implementing registry, but it's just not something I like to do generally....


    kyberfabrikken, the thing is, I really don't know how many objects will need the Configuration object, but it could potentially be a lot. What do you mean about include_path though? i don't see how that applies to configuration? Also what's your view about the constant i mentioned?

    I don't understand what is wrong with using just one constant for finding the path to the application. To me, it makes it more flexible and easier to find files that need included.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana
    What do you mean about include_path though? i don't see how that applies to configuration? Also what's your view about the constant i mentioned?
    If you set include_path to point to where your files are, you don't need a constant to point there.

  8. #8
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah that's true.

    So, passing that issue, what of the Configuration object? What are some of the better options for doing this?

    Seems to be a compromise either way.

  9. #9
    SitePoint Member blanks777's Avatar
    Join Date
    May 2004
    Location
    Midlothian, VA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I personally use only one constant, 'SITE_PATH', in my index file that gets used when needed. I could pass it into my application constructor... but I don't think that's neccessary (doesn't sound like a bad idea).

    I use the registry as a singleton, as well. I put my request, response, db & config objects in there (and anything else I'll need as I go on). I don't have to actually call the registry on all my pages that require a db access, however. I have a higher class that I just extend from with an instance to that.

  10. #10
    SitePoint Member
    Join Date
    Dec 2005
    Location
    Moscow
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's the use of APPLICATION_PATH constant? Proper design should incapsulate this behaviour in a say ApplicationFile class.

  11. #11
    SitePoint Member
    Join Date
    Dec 2005
    Location
    Moscow
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As for Registry I have the root class that is inherited by all classes in the framework.

    PHP Code:
    class Object
    {
        
    /*
         * PHP4 __construct() implementation
         */
        
    function __construct() {}

        function 
    Object()
        {
            
    $args func_get_args();
            
    call_user_func_array(array(&$this'__construct'), $args);
        }

        function &
    getInstance($classname)
        {
            return 
    Registry::getInstance($classname);
        }

        
    /**
         * @param string class name or registered alias
         * @param string method the method name to invoke
         * @param mixed* arguments passed to method
         */
        
    function invoke($classname$method)
        {
            
    $args func_get_args();
            return 
    call_user_func_array(array('Registry''invoke'), $args);
        }


    That way client classes have no dependency of a particular Registry implementation.

  12. #12
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana
    So, passing that issue, what of the Configuration object? What are some of the better options for doing this?

    Seems to be a compromise either way.
    What kind of information do you have in this configuration object ? Can't you pass the information in constructors to the objects that need it ?

  13. #13
    SitePoint Enthusiast jameso's Avatar
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Omut
    As for Registry I have the root class that is inherited by all classes in the framework.
    Omut, would you be able to give an example of the usage of this class?

    Thanks very much!

    James

  14. #14
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Omut,

    What would such an ApplicationFile class do? All this constant is is an absolute path to the location of the application. I'm curious what you mean, though.

    I did attempt dropping the constant, but now where an absolute path is needed, I have to use dirname(__FILE__). For includes though, having it in the include path is working fine.

    kyberfabrikken,

    Just general configuration. I can pass it to the objects that need it, i was just wondering if this is the best way, especially if a lot of objects require it.

  15. #15
    SitePoint Member
    Join Date
    Dec 2005
    Location
    Moscow
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class Action_Page extends Object
    {
        function 
    doAction()
        {
            
    $response =& $this->getInstance('Http_Response');
            
    $response->ifEtagMatchExit(); //304
            
    $response->setStatus(200); // assume OK

            
    $uri  $this->invoke('Http_Vars''getPageName');
            
    $html $this->invoke('Xml_Xslt''generate'$uri$this->getInstance('Xml_PageHelper'));
            if ( !
    $this->isDebugMode() ) {
                
    $this->invoke('Http_Cache''write'$uri$html);
            }
            
    $response->sendContent($html'text/html; charset=windows-1251');
            return 
    true;
        }


    This is a real life example of a complex method, it should probably be refactored.

    ---
    You can view some more code of this framework. source.zip

  16. #16
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana
    Just general configuration. I can pass it to the objects that need it, i was just wondering if this is the best way, especially if a lot of objects require it.
    But what exactly is general configuration ?
    Can you give some concrete examples ?

  17. #17
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Omut
    As for Registry I have the root class that is inherited by all classes in the framework.
    Code smell. Refactor that. You'd be better off using composition than inheritance here.

  18. #18
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken,

    One specific thing it includes is the mapping between requested actions and their associated class, views, etc. Also probably DB connection information.


    BerislavLopac,

    I agree. Just using composition for everything seems like it'd get out of hand. I mean using it for all dependencies.

  19. #19
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana
    One specific thing it includes is the mapping between requested actions and their associated class, views, etc. Also probably DB connection information.
    Both things sounds like they would be better solved by passing some specific options to a constructor.
    In my experience, configuration files add complexity. Assembling the objects explicitly is a lot easier to understand for a programmer, and since your audience are likely to be programmers, this seems like the best choice.

  20. #20
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana
    I did attempt dropping the constant, but now where an absolute path is needed, I have to use dirname(__FILE__). For includes though, having it in the include path is working fine.
    I don't see any problem with have a constant that holds the absolute path to the application. It is a "global" true, but that's a really moot point when the location of the application is something that is generally going to be "global" to the entire application, and once defined you cannot change the value of it. It is also going to be on the order of 1,000 magnitudes faster than using dirname(__FILE__) all over the place. In a project I am working on, at first I started using dirname(__FILE__) where needed, but then I did some profiling, and the application spent most of its time in dirname(__FILE__) !! Even more time in there than in the database, and the application makes good use of the database! (it is an ecommerce app) Strange but true.

  21. #21
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    It is a "global" true, but that's a really moot point when the location of the application is something that is generally going to be "global" to the entire application ...
    That's what they all say, about globals

    But you're right - Since classes can't be undefined in PHP, including classes will always have global reach. I just don't see the need though - PHP is born with an APPLICATION_PATH constant - it's just called include_path. No need for other constants, or dirname(__FILE__).

  22. #22
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken,

    Interesting. how would you suggest passing this data then? Firstly, how would you map requested actions to their respective classes and associated views? Secondly, how would you pass database connection information in a way so it doesn't need to be entered in multiple locations?

    Every application i've used had a configuration of some kind, so I'm not quite used to that idea of not having a configuration. But I am quite interested to hear what you have to say about it.

    dreamscape,

    Wow, quite interesting. I agree calling a function has to be slower than using a constant. I didn't realize how much slower, though. Interesting response.

  23. #23
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana
    Interesting. how would you suggest passing this data then? Firstly, how would you map requested actions to their respective classes and associated views?
    I can't answer that explicitly, without knowing how your application looks. But I would do it programmatically somehow.

    Quote Originally Posted by devbanana
    Secondly, how would you pass database connection information in a way so it doesn't need to be entered in multiple locations?
    The question is rather how you will get a single object to multiple locations. Because once you answered that, you don't need to share the connect-information, since you can create a single object and reuse that.
    I use a registry of factories for this task. It has the bonus of decoupling concrete class dependency (Not that that's a big issue, but heck - if I get it for free ...)

  24. #24
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Well, what I was planning to do was something like:

    The front controller gets the request. It gets the first part of the requested action and through configuration knows which class that is associated with, so passes the request onto that handler.

    I'm curious how you are implementing that registry. Do you have a compact example or some explanation of it? I'm very interested in "decoupling concrete class dependency", as well.

    I understand what you are saying about having one object and passing it around (for the database connection), but it has to be instantiated somewhere; where's that connection information coming from? Where's it being instantiated?

    P.S.: I am genuinely interested in this. I truthfully hate configuration, because it is so awkward. I don't want to store it as a simple set of constants or an array because that is a lot of global information and is just awkward. XML files are nice, but, though they are elligant in use, they ar einsecure (especially if you want to store database information somewhere). I then thought of an SQLite database, but configuration data isn't very fitting for a database, unless you break normalization, or are willing to deal with inefficiencies.

  25. #25
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,

    kyberfabrikken, I was looking at your framework just to see how you implemented this, but I've not come out any clearer.

    What I'm especially interested in is, how do your controllers know what their child controllers are? It doesn't look to be hardcoded, so where does this information come from?

    Mine is similar in that there is a hierarchy of controllers/handlers; I just want to know how, without configuration, they are supposed to know what their child controllers are.


Bookmarks

Posting Permissions

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