SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Can we go to a 48 hour day?
    Join Date
    May 2002
    Location
    MI
    Posts
    906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trouble accessing global $CONFIG in classes

    I am trying to set up a single config.php to use across my project. It's my first time trying this out.

    I have my config.php something like this:
    GLobal $CONFIG;
    $CONFIG = array();
    $CONFIG['Permissions'] = array(
    "Admin",
    "Submitter",
    "Committee",
    "UserAccountAdmin"
    );

    Now I can't seem to access the Permissions value in a class file I am using. I have a login class that I want to pull in these possible permissions to assign people.

    Any idea where I am going wrong. I thought if I declared $CONFIG global I would not have to worry about scope later on.

    Code from class:
    $this->PERMISSIONLIST = array();
    $this->PERMISSIONLIST = $CONFIG['Permissions'];

    var_dump($this->PERMISSIONLIST); // this is getting NULL

    Thanks for the help.
    mitechie.com
    "Techies just think a little differently
    ...at least that is what they keep telling me."

  2. #2
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to declare $CONFIG as a global variable in every function it's being used. Including functions inside classes. "global" has no effect outside functions.

    PHP Code:
    $CONFIG = array();
    $CONFIG["Permissions"] = array(
      
    "Admin",
      
    "Submitter",
      
    "Committee",
      
    "UserAccountAdmin"
    );

    class 
    TheClass {
      function 
    TheClass() {
        global 
    $CONFIG;
        
    $this->permList $CONFIG["Permissions"];
      }


  3. #3
    Can we go to a 48 hour day?
    Join Date
    May 2002
    Location
    MI
    Posts
    906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops, thanks for the pointer.
    mitechie.com
    "Techies just think a little differently
    ...at least that is what they keep telling me."

  4. #4
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have to use it in a punch of places.... add it to $GLOBALS

    Then instead of using global $CONFIG, just call $GLOBALS['CONFIG']['CONFIG_ARRAY_KEY_HERE'] in your function wherever you need to use it.

    - Eric
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us


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
  •