Skip navigation

This is a bit of a weird problem, but say you’re trying to convert over to being all nice and OOP in your php code. Suddenly though, you notice some weird things. You can’t seem to access your global variables from within member functions!

For instance, say you have this include file called ‘names.php’ which has a huge global array in it called $names.

Attempt #1

in file names.php
$names = array(
  'bob', 'john', 'bobo', 'bobobobo'
);
require_once( 'names.php' );

class User
{
  private $name;
  function __construct()
  {
    $this->name = $names[ 0 ] ; # php can't access $names array,
    # even though its a global variable in 'names.php'!
  }
}

Attempt #2

require_once( 'names.php' );
global $names; # a good step, but still not enough

class User
{
  private $name;
  function __construct()
  {
    $this->name = $names[ 0 ] ; # php STILL can't access $names array,
    # even though its a global variable in 'names.php'!
  }
}

Attempt #3

require_once( 'names.php' );
global $names; # a good step, but still not enough

class User
{
  private $name;
  function __construct()
  {
    global $names; # the second piece of the puzzle.
    $this->name = $names[ 0 ] ; # ah, finally it works!
    # we had to have the global declaration in TWO places!
  }
}

In fact in PHP, the same goes for regular (non-object) functions as well. Need to declare global $variableName both after include AND inside function that uses the global.

Advertisements

6 Comments

    • Jason
    • Posted October 22, 2008 at 11:53 am
    • Permalink

    *bump* (for people that don’t know and might come accross this)

    The global statement only needs to be used within the class/function so PHP knows it has to reference the variable from the outside (global) environment instead of within the class/function.

    require_once( ‘names.php’ );
    class User
    {
    private $name;
    function __construct()
    {
    global $names;
    $this->name = $names[ 0 ] ;
    }
    }

    • Alex
    • Posted November 15, 2011 at 3:35 pm
    • Permalink

    You don’t really need to put “global” twice. Just declare the variable prefixed by the “global” statement in the function/method that needs to access the data.

    Greetings.

    • Anonymous
    • Posted January 11, 2012 at 7:15 pm
    • Permalink

    You can also use ‘global $var’ atop the file you store you class in. For example,

    global $val;

    class Test {

    function test() {
    echo $val;
    }

    };

    • Anonymous
    • Posted February 21, 2012 at 9:44 am
    • Permalink

    you cant use global $var ATOP the file, see attempt #2

    • Anonymous
    • Posted October 15, 2012 at 8:12 pm
    • Permalink

    Thanks so much… great example….

  1. Thanks man, just what i needed.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: