SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML config files?

    Recently I was reading some article on OO software development (forgot the link) and it said it's prefferable to have configuration files in XML format. is this feasible in PHP?

    I'm curious because, although it would greatly help organize the structure of a configuration file, it seems like alot of overhead would be induced from having to parse the file and then translate it into to appropriate php data (i.e. mysql connection info, globals, etc).

    I mean, I usually have my configuration files contain somehting like

    PHP Code:
    <?php

    define
    ('MYSQL_HOST''localhost');
    define('MYSQL_DB''datbasename');
    ...

    define('FS_ROOT''/home/username/public_html/');
    ...
    ?>
    And then use those constants in my program, like when a DAO is initialized. I've had no trouble with this, but just curious if it would be feasible to use an XML file since it makes sense even to a laymen.

    still, it seems like it's not very good towards a language such as PHP, since, unlike any other app which would simply read the xml file at startup time and never have to again, with PHP you pretty much have to read it upon each request, unless you use some other means such as a database (which you'd still have to induce the overhead of reading from the database).

    Thoughts?

  2. #2
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're using PHP 5, they've got libxml2 and even SimpleXml, either of which will help your program.

    libxml2 will be faster, but SimpleXML will take far less code. It all depends on what you want -- I would go for faster over less code, just for scalability purposes...

    You could load your configuration from XML and then cache the configuration data, which would reduce the need for parsing the XML files so often.
    Ryan

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The one thing you may want to consider is that in PHP this configuration file will have to be parsed each and every request, whereas with something like java where there is an application container, the configuration would only have to be parser each time the server is started. You may want to evaluate the impact of that on your overall application performance.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would wait until PHPversion5 SimpleXml, and use INI style configurations.

    I've used XML before and there can be problems if your configuration file has deep nodes, in regards to the amount of effort required for PHP to parse it

  5. #5
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it's all about its disadvantages, but does it has some advantages?

  6. #6
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only advantage I can see for using XML is that it is a more widely accepted format than PHP is, meaning that the chance some client knows XML syntax is a bit greater than the chance the client knows PHP syntax.

    Anyway, it is feasable of course, but parsing XML takes time. Caching could be an option, but has the disadvantage that your code for reading configuration options has to be aware of the caching: ie. It has to check if a cached version exists and if it's still valid.

    Maybe a slightly different approach could be that you use XML as the syntax for the source files of configurations. That way, most of the runtime code doesn't have to know about caches and all that, you put that into an administration backend where xml config data can be uploaded.
    When uploaded (and validated!) the XML can be transformed into something more friendly to PHP (a serialized associative array written to disk for example).
    This is of course also a process that could be done offline.

    Just a thought.
    Per
    Everything
    works on a PowerPoint slide

  7. #7
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it's an issue of clients manually editing it, use INI files.

  8. #8
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    The one thing you may want to consider is that in PHP this configuration file will have to be parsed each and every request
    Couldn't parse the XML data into an associative array of sorts (or a "bean" object) and then cache that in a PHP include or ini file until you detect that the XML file has been edited? That way, you are getting the best of both worlds. That way you only have to parse maybe once every n requests (where n is a function of your visitors and the motivation of your application development staff).

    Easily readable XML files, and super fast PHP includes.
    Ryan

  9. #9
    SitePoint Enthusiast NativeMind's Avatar
    Join Date
    Aug 2003
    Location
    USA
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think an XML configuration file is OK if only 2 conditions are met:

    - the data to be stored is complex with many nested elements (which would make an ini-file look horrible)

    - you can initialize these settings once per session and store the relevant information in the session data.

    Anything else, I go for an ini files... they are easy to edit, and they give you the most bang for your buck putting all the data into an array in one simple function. Hey, arrays are one of the most powerful features in PHP.

    By the way, there's nothing wrong with having multiple configuration files for different branches of processing. I.e. if your application has various modules, each one can have their own ini file. If you set it up right, it can be a nice polymorphic loading of data.

  10. #10
    Team SitePoint Lucas Chan's Avatar
    Join Date
    Sep 2002
    Location
    Melbourne
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dunno guys, the only advantage that has been mentioned in this thread so far is:

    Quote Originally Posted by been
    meaning that the chance some client knows XML syntax is a bit greater than the chance the client knows PHP syntax.
    To me, it sounds like an awful lot of overhead for very little gain.

    I suspect that was sweatje says is true. For development environments like Java where you have a stateful application container, an XML config file (that is parsed just once) would probably make sense.

  11. #11
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lucas Chan
    To me, it sounds like an awful lot of overhead for very little gain.
    Well, to be honest, to me also, although I'd rather call it 'complexity' instead of 'overhead', the overhead would actually be minimal if you can keep the xml parsing to a minimum (ie. only when the xml gets updated).

    In a way it's a bit like the 'Yet Another Template Discussion'.
    XML has the benifit of being a widely accepted language, given a dtd, xml configuration files can be created and validated by clients, without having the need for a php parser, it could even be a requirement of an application, in which case the added complexity (or overhead as you call it) is simply a necessity.
    Per
    Everything
    works on a PowerPoint slide

  12. #12
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about serialization as a form of caching? for a complex config file that gets parsed into a complex data structure, you could parse the XML once, then serialize the config object for whatever scripts need to use it. of course, there is still the issue of deciding when to re-serialize the config, not to mention what kind of overhead serialization itself brings to the table.

    i think the ultimate format of the config file is just as much a function of how often it needs to be parsed as it is how often it will be written...not to mention HOW it will be written and WHO will do the writing. if the developer is the only person making adjustments to the config file, i would be inclined to do as OfficeOfTheLaw and just have a PHP include with the constant declarations. it's easy enough for a PHP developer to edit a PHP script, and there's only the processing overhead of an include(). if, on the other hand, the config is something that users of the system will be making changes to, you will have to sacrifice a little bit of performance and developmer convenience for user convenience (ie, INI, XML, etc...depending on the app's architecture)

  13. #13
    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)
    well there's yet a solution. You could write your config's in xml, and then parse them into php-code. you could invoke the parser manually, or you could just do it the first time the file is used, and then save (cache) the generated php-code.

  14. #14
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (Rather preliminary) benchmarks lead me to suspect that the overhead between reading a serialized hash from file imposes no overhead compared to an include...
    As a matter of fact: As the configuration file grows larger, reading it and deserializing a hash from file seems to be faster, here are my results so far.

    The benchmark files consist of: 2 config files, one small, one a bit larger, the small one is:
    PHP Code:
    $cfg = array(
         
    'db' => array(
             
    'default' => array(
                 
    'type' => 'my',
                 
    'user' => 'bobo',
                 
    'pass' => 'bobo',
                 
    'name' => 'bobo',
                 
    'host' => 'localhost'
             
    ),
             
    'office' => array(
                 
    'type' => 'pg',
                 
    'user' => 'bobo',
                 
    'pass' => 'bobo',
                 
    'name' => 'bobo',
                 
    'host' => '192.168.0.10'
             
    ),
             
    'test' => array(
                 
    'type' => 'my',
                 
    'user' => '',
                 
    'pass' => '',
                 
    'name' => 'test',
                 
    'host' => 'localhost'
             
    )
         ),
         
    'admin' => array(
             
    'name' => 'bobo',
             
    'email' => 'bobo@example.com'
         
    ),
         
    'paths' => array(
             
    'modules' => '/home/bobo/modules/',
             
    'templates' => '/home/bobo/templates/',
             
    'cache' => '/tmp/bobo/wwwcache/'
         
    )
     ); 
    The large configuration file repeats that similar structure (different keys) about 10 times.
    Then, there are a couple of different files: 2 working directly with the global $cfg from the config file, they just do an include and a print_r, one works with the smaller file, and one with the larger.
    2 working on both config files, but they check if a 'compiled' file exists, if it does they read it and unserialize it, otherwise they include the source, serialize it and write it to disk.
    PHP Code:
    $cfg_file '/tmp/spfbench_compiled_configuration.tmp';
     if (! 
    is_file($cfg_file)) {
         include 
    'config.inc.php';
         
    $data serialize($cfg);
         
    $fh fopen($cfg_file'wb');
         
    fputs($fh$datastrlen($data));
         
    fclose($fh);
     } else {
         
    $cfg unserialize(file_get_contents($cfg_file));
     }
     
     
    print_r($cfg); 
    Third group reads and unserializes from file, no check, just $cfg = unserialize(...).

    The benchmark 'frontcontroller' (just to have the mvc discussion shaken, not stirred ) is a shell script that calls ab -100 on all the scripts.
    What I've found:
    - small config file:
    Allmost no difference, native include is about 0.5 req/second faster, but serialized is about that same figure faster if no check is made and the data is directly read from file and unserialized. Ab comes in at around 35-40req/second. I do not think this difference is significant at all.

    - large config file:
    With checking, the serialized version is around 1.5 req/second faster, without the check it's about 2 req/second faster. I suspect that, as the config file grows, this difference will grow too, but I also think that, if a config file grows that large that the speed difference becomes an issue, you've got other troubles on your hands.
    Ab comes in at around 15-20 req/second.

    The benchmarks are preliminary because:
    - The test machine is really old (K6II-350/128Mb)
    - The PHP version is really old (4.1.2, please don't kill me )
    - The benchmark is simple, it doesn't really 'cache', rather 'compile once', which is what I was trying to test, but your requirements may vary of course...
    - Some will regard the benchmark as unfair, since the 'compile' version doesn't parse any xml, which is true, but really all I wanted to test was the speed difference include/serialize

    All in all, it seems that reading serialized hashes from disk would be a viable solution to store parsed xml, in php the code would also be simpler I reckon, since you'd parse the xml to a hash, serialize it and write it to disk. On the other hand, maybe the XML could be directly transformed with XSLT to any format you like (maybe PHP's serialized version of the hash, if you tend to find out it is slower than includes)...

    omg, I'm still making sense here?
    Per
    Everything
    works on a PowerPoint slide

  15. #15
    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 been
    ... maybe the XML could be directly transformed with XSLT to (...) PHP's serialized version of the hash ...
    now, that's a challenge. could be kind of cool tho'.

    Anyway, I saw a comparison between storing data in defines, hash's and as global variables somewhere. unfortunately i don't remeber where i saw it, witch kind of makes it useless in this relation, but i remember the conclusion. global variables are fastest, and defines and hash's are about the same. at first i were a bit puzzled that defines scored as bad as they did, but when you think of it, this makes sense. in C/C++ defines are pre-processor instructions, and therefore vastly faster to use than a variable. PHP inherited the syntax of defines from C, but in PHP they are realy just treated as variables.

  16. #16
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    well there's yet a solution. You could write your config's in xml, and then parse them into php-code. you could invoke the parser manually, or you could just do it the first time the file is used, and then save (cache) the generated php-code.
    I seriously feel as if I'm transparent on these forums. I've already said this twice.
    Ryan

  17. #17
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    now, that's a challenge. could be kind of cool tho'.
    Is this what you're looking for?

    http://www.sitepoint.com/forums/show...13&postcount=5
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  18. #18
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by Gybbyl
    I seriously feel as if I'm transparent on these forums. I've already said this twice.
    Yes, but he said it like this (hand moves up and down)

    Commercial from a couple of years back that got immitated quite a bit in our staff meetings.

  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)
    I seriously feel as if I'm transparent on these forums. I've already said this twice.
    i meant something like this :
    Code:
    <xml>
    <config>
        <foo value="bar" />
        <boo value="baz" />
    </config>
    would become this :
    PHP Code:
    <?php
        define
    ('foo''bar');
        
    define('boo''baz');
    ?>
    as opposed to merely serializing the data.

  20. #20
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Off Topic:

    Quote: Gybbyl

    I seriously feel as if I'm transparent on these forums. I've already said this twice.

    Yes, but he said it like this (hand moves up and down)

    Commercial from a couple of years back that got immitated quite a bit in our staff meetings.
    haha, I might have known! I need to add pizzaz to my responses
    Ryan

  21. #21
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Brazil
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by been
    (Rather preliminary) benchmarks lead me to suspect that the overhead between reading a serialized hash from file imposes no overhead compared to an include...
    I redid been's tests and found them to hold. Actually serialize went quite better on that 'small' structure he gave as example 140 reqs/sec x 163 reqs/sec...
    And there is the extra print_r time which should be the same for both...
    AMD 750, 384Mb, php 4.3.8, turckMM Cache on (i expected includes to be much faster with this on)...
    Last edited by nuncanada; Aug 12, 2004 at 16:26.

  22. #22
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Germany
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think XML config files in php are more hype then usefull.

    Is use a singelton config object:

    Config File looks like that:
    (group, var, value)
    $OBJ_CONFIG->set('db', 'type', 'mysqli');
    $OBJ_CONFIG->set('db', 'host', 'localhost');
    $OBJ_CONFIG->set('db', 'user', 'root');
    $OBJ_CONFIG->set('db', 'password', 'xxxxxx');
    $OBJ_CONFIG->set('db', 'db', 'framework');
    $OBJ_CONFIG->set('db', 'port', 3306);

    It's easy to use and fast.

    Another disadvantage is a possible security risk: If the xml config file is in your root directory then it is perhaps accessible to webusers, because it is plain text. A php file would be parsed!

    Greetings Sambox

  23. #23
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sambox
    Another disadvantage is a possible security risk: If the xml config file is in your root directory then it is perhaps accessible to webusers, because it is plain text. A php file would be parsed!
    But you can restrict access to your XML configuration file!

  24. #24
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Or you would put your config file above webroot which you should really do anyway. PEAR::Config can store your config data in an XML file, if you choose to do this why reinvent the wheel?


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
  •