SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    set global array vars or use a config object

    I'm revamping a huge php app for a client in php 4.3 on linux (no chance to upgrade to php5). It is currently very procedural, hardly any functions and no objects. There are a ton of variables floating around (at least 100) and I'm looking to categorize them into namespaces a little better. I thought about creating a Config object that would hold all this info, there wouldn't really be any methods in it though except my getters/setters. Would it be more beneficial to just stake out a claim in $GLOBALS, something like:

    PHP Code:
    $GLOBALS['webVars'] = array();

    // Email vars

    $GLOBALS['webVars']['emails'] = array(
                                            
    'tech'  => 'tech@sample.com',
                                            
    'admin' => 'admin@sample.com'
                                       
    );
    // DB conn vars
    $GLOBALS['webVars']['db'] = array(
                                            
    'host'  => 'localhost',
                                            
    'db' => 'test',
                                            
    'user' => 'username',
                                            
    'password' => 'password'
                                       
    ); 
    What are the performance trade-offs with either method? What are the security issues with either method?

    Thanks.
    -RZ

  2. #2
    SitePoint Enthusiast konky2000's Avatar
    Join Date
    Mar 2003
    Location
    Oakland
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about using constants? I know you can't use arrays, but since constants are usually defined on a single page, I find it is usually pretty easy to track them down when I am reading PHP code.

    PHP Code:
      define'EMAIL_DOMAIN''@gmail.com' );
      
    define'EMAIL_ADMIN''webmaster'.EMAIL_DOMAIN );
      
    define'EMAIL_EDITOR''editor'.EMAIL_DOMAIN );
      
      
    define'DB_HOST''localhost' );
      
    define'DB_PUB_USER''cd2com_public' );
      
    define'DB_PUB_PASS''cd2com' ); 
    Konky 2000 Collections - Japanese stickers and floaty pens

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good suggestion but using constants won't work in this case because some of the values will need to be changed on the fly. Sorry, should have mentioned that.

  4. #4
    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)
    Using a Singleton Configuration object is the standard Advanced PHP (OOP and Pattern buzzword compliant) answer.

    off the cuff:
    PHP Code:
    // PHP4
    class Config {
      var 
    $stuff = array();
      function &
    getInstance() {
        static 
    $instance;
        if (!
    $instance) {
          
    $instance = array(new Config);
          
    //some other setup stuff, read config file, etc
        
    }
        return 
    $instance[0];
      }
      function 
    getOption($key) {
        @return 
    $this->stuff[$key];
      }
      function 
    setOption($key$val) {
        
    $this->stuff[$key] = $val;
      }
    }

    // usage
    $config =& Config::getInstance();
    $email $config->getOption('admin_email'); 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    SitePoint Zealot Krieger's Avatar
    Join Date
    Dec 2003
    Location
    Nevada - USA
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A BIT OFF TOPIC:

    Jason, will examples such as these be in your new book (I'm a relative php and total OOP newcomer)?

    Sorry for the hijack, but this is good stuff
    Verwüstung rufen und des Krieges Hund' entfesseln!

    My Site
    My first php/MySQL project -->Thanks Sitepoint members

  6. #6
    SitePoint Addict mx2k's Avatar
    Join Date
    Jan 2005
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you use arrays and other people are using the app, it might look better if you do something like
    $cfg['appName']['version'] = '';
    $cfg['appName']['debuggerMode'] = true;
    $db['host'] = 'localhost';
    $db['user'] = 'bobHope';
    $db['host'] = '******';

    however i would keep the db variables outside web root folder
    and not make those globals, or session available
    in other words only make those avaiable only as needed, you would have to call those variables only when a script needed them and you can stash them into an array for portability.

    this way you can set $_SESSION = $cfg; and not have to worry about it putting your db variables into the session.

    then use a fucntion __construct($host, $user, $pw, $db ){ to put your variables into the class

    $my = new mysql($db['host'], $db['user'], $db['password'], $db['database']);

    i tend to use a combination of constants and arrays

    i usually leave constants for things like files

    include PTX.CORE.DATABASES.DBCONNECTION;

    ptx is the path to my application
    core is the path to the core folder in the ptx application
    databases is the path to the database folder in the core folder
    dbconnection is location of the class in the database folder

    then things like e-mail and what not, i leave as $cfg['email']['admin'] = 'me@myisp.com';

    and i try to go from broad to more detailed using arrays $cfg['email']['usertype']

    or mass mail the important people $emailList= $cfg['email'];

    foreach ($emailList as $key => $value) {
    $mailto .= $value.',';
    }

  7. #7
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Using a Singleton Configuration object is the standard Advanced PHP (OOP and Pattern buzzword compliant) answer.
    Nice example... this was my first thought on this but I could definitely use the simplicity of something like:

    Quote Originally Posted by mx2k
    or mass mail the important people $emailList= $cfg['email'];

    foreach ($emailList as $key => $value) {
    $mailto .= $value.',';
    }
    Being able to roll thru a specific type of config var like this would be handy, I guess I could still do this with the singleton though, either create multiple instances of the config object, or just created a deeper array in the class itself.

    Do you think it's more overhead to instantiate an object as opposed to just populating the globals (or another) array? The site(s) that I'm working on really get hammered, 100k plus unique visits per day.

    Thanks...good info.
    -RZ

  8. #8
    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)
    Quote Originally Posted by Krieger
    Jason, will examples such as these be in your new book (I'm a relative php and total OOP newcomer)?
    Absolutely, Including things like why is there a goofly looking return $instance[0] in there?

    The way I am writing it, the book is chalk full of code, including unit testing done in SimpleTest. Trying to pull together as practical of an example as possible which shows each pattern in relative isolation.

    Not doing any intro to OOP in it (a little on php4 -> php5 OOP), but there are lots of other good web and dead tree resources for that.

    HTH

  9. #9
    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)
    Quote Originally Posted by richz3508
    Nice example... this was my first thought on this but I could definitely use the simplicity of something like:
    You could just as easily do:
    PHP Code:
    foreach($config->get('email_list') as $email) { //...

    as long as whatever you set there was an array
    PHP Code:
    $config->set('email_list', array(//...
    )); 
    Quote Originally Posted by richz3508
    Do you think it's more overhead to instantiate an object as opposed to just populating the globals (or another) array? The site(s) that I'm working on really get hammered, 100k plus unique visits per day.

    Thanks...good info.
    -RZ
    I think how you load up the configuration object (parsing a file, extacting from a db, etc) will be far more of a load concern than the costs of dereferencing an object vs. a global variable. Remember there is dereferencing costs with deeply nested array as well as with objects.

    HTH

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    either create multiple instances of the config object,
    Not with a Singleton you won't The whole point of the Singleton is that you'll only ever have one instance to it.

    Maybe your talking about using the Singleton a number of time it's self in your script, in which case then that isn't a problem.

    SweatJe,

    I do remember now that you were talking about a book, when are you publishing it? Btw, where are the free chapters? Eh?

  11. #11
    SitePoint Zealot Krieger's Avatar
    Join Date
    Dec 2003
    Location
    Nevada - USA
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    SweatJe,

    I do remember now that you were talking about a book, when are you publishing it? Btw, where are the free chapters? Eh?
    Here you go (no free chapters though).

    I already have a copy on order, and am looking forward to digging into this material.
    Verwüstung rufen und des Krieges Hund' entfesseln!

    My Site
    My first php/MySQL project -->Thanks Sitepoint members

  12. #12
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm...

    Thanks for the link

    Since at this time anyways, there are no sample chapters to get an impression of the book I think I'll wait a bit longer than the release date. No offence to yourself SweatJe of course, and certainly not questioning the books quality on content.

    Me being me I'd would have liked to read up on an early chapter or something like that, now I'll wait... Maybe the publisher will release a chapter huh?

  13. #13
    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 Dr Livingston
    Since at this time anyways, there are no sample chapters to get an impression of the book I think I'll wait a bit longer than the release date. No offence to yourself SweatJe of course, and certainly not questioning the books quality on content.

    Me being me I'd would have liked to read up on an early chapter or something like that, now I'll wait... Maybe the publisher will release a chapter huh?
    I have not talked a lot about the marketing strategy with them. Since they have given sample articles from the php|architect magizine, it seems likely they would do a chapter from the guidebook.

    I'll let you know more as I find out myself.

    BTW, Marcus Baker (lastcraft) is doing an outstanding job of technical review on it as well.

  14. #14
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you refactoring the whole lot into OOP? If so, I think you'll find that the problem will just disappear all on its own. Once you start encapsulating everything will tend to drift into its proper place - maybe a few constants left over.


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
  •