Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with a config updater function

    ok, basically what i want is this. i want a global config file, with a $config[] array, and a function to update that array. such as.

    PHP Code:
    <?

    $config
    ['name'] = "Justin";

    function 
    changeSetting($setting$value) {

      
    $config[$setting] = $value;

    }

    ?>
    when i include that into my page, and run the following, it stays as 'Justin'

    PHP Code:
    echo $config['name'];
    changeSetting("name""Bob");
    echo 
    $config['name']; 
    and i doing this right, or do i need to make this into a class...

    coming from asp.net, im willing to learn oop in php.

    Thanks, Justin

  • #2
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not just change the value directly without the use of a function. The function in itself without being inside of a class is not really oop anyway. if you really want oop try this (based on php4):
    PHP Code:
    class configKey {
       function 
    configKey($key,$value) {
           
    $this->setKey($key);
           
    $this->setValue($value);
       }

       function 
    setKey($key) {
           
    $this->key $key;
       }

       function 
    setValue($value) {
           
    $this->value $value;
        }

    You can then use it like so
    PHP Code:
    $config['name'] = 'Justin';
    $cfgObj = new configKey('name','Bob');
    $config[$cfgObj->key] = $cfgObj->value
    *I'm fairly new with PHP as well. I've been programming less than 1 year in any given language but have picked up php on my own. I'm getting a better grasp on OOP though I know there is still much more that I need to learn. I welcome better suggestions so that I may learn as well from more seasoned programmers.
    Last edited by martialtiger; 01-31-2007 at 05:59 AM.

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    the reason your function doesn't work is that $config you're setting inside the function isn't the same $config that's in the rest of your page.

    What you've started to do makes sense, in that setting values directly to the array is a little 'brittle', but the post above is hinting towards a better way, but is (in my opinion) a little off...
    I would use something like:
    PHP Code:
    class Config {
      private  
    $data = array();
      public function 
    __construct() {

      }

      public function 
    set($key,$val) {
        
    $this->data[$key]=$val;
      }

      public function 
    get($key) {
        if(isset(
    $this->data[$key])) {
          return 
    $this->data[$key];
        }
        return 
    false;
      }

    and then you would use
    PHP Code:
    $config = new Config();
    $config->set('name','Bob');
    echo 
    $config->get('name');
    //etc. 
    I would probably make this class into a singleton (http://www.developertutorials.com/tu...729/page1.html) but it's a good start
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for the reply, i think i will be using this, i am really trying to get my head into oop with php.

    Thanks alot, Justin


  •  

    Posting Permissions

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