#1
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115

    Object seems to behave like an array?


    Hi guys,

    since i am playing around with oop i noticed something useful (i think). When I did a var_dump on a config class like below I got pretty much the same result as with an associative array. May I assume this is could be useful for config settings or is it better to maybe use a define() for these settings. I kind of like this object approach, since i can use the autoloader.
    PHP Code:
    class Config {
        public 
    $offline '0';
        public 
    $display_offline_message '1';
        public 
    $offline_image '';
        public 
    $editor 'tinymce';
        public 
    $captcha '0';
           
    // .....          etc

    P.s. this is part of a joomla config file for those interested.

    -edit: yes I really think this can be very usefull, just discovered the get_class_vars() function which could be very useful for loading configuration variables I think. Any thoughts?
    Last edited by aeternus; December 29th, 2012 at 04:56 PM.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    sure, you can use classes to store configuration values. However, you should use constants if the values are not supposed to change during runtime.

    If the configuration is supposed to be "human readable", you might also use a specialized language like YAML or JSON (or XML) instead of PHP scripts. This lets you get rid of all the code overhead.
  4. #3
  5. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by Jacques1
    Hi,

    sure, you can use classes to store configuration values. However, you should use constants if the values are not supposed to change during runtime.

    If the configuration is supposed to be "human readable", you might also use a specialized language like YAML or JSON (or XML) instead of PHP scripts. This lets you get rid of all the code overhead.
    Thanks as always Jacques!
    I haven't started that subject yet (as far as class constants), but It makes sense. This kind of data isn't meant to be changed indeed You mean the class constants? or global defined constants? (someone said the later were slow)

    Although I have no knowledge of code overhead reading from an xml file into php sounds more costly (without anything to back that feeling up ).

    Thanks again!
    Last edited by aeternus; December 29th, 2012 at 05:36 PM.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    I mean class constants. They're simply defined in the class body with the "const" keyword.

    Originally Posted by aeternus
    Although I have no knowledge of code overhead reading from an xml file into php sounds more costly
    Well, does it matter? If you actually do experience performance problems, they're unlikely to be caused by the application itself, let alone the parsing of some lines of YAML.

    So don't avoid this just because you think it's expensive.

    With "code overhead" I mean everything that isn't relevant for the configuration data itself and merely exists for the PHP interpreter (keywords, function calls etc.)
    Last edited by Jacques1; December 29th, 2012 at 06:06 PM.
  8. #5
  9. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    okay, cool. Thanks for the tips! I'l make sure i'll use them

    -edit: that YAML looks cool! (too bad i can't run it very easily on windows) i'll might try it with a virtualmachine
    Last edited by aeternus; December 29th, 2012 at 06:17 PM.

IMN logo majestic logo threadwatch logo seochat tools logo