38

I have a global variable outside my class = $MyNumber;


How do I declare this as a property in myClass?
For every method in my class, this is what I do:

class myClass() {

    private function foo() {
         $privateNumber = $GLOBALS['MyNumber'];
    }

}



I want this

class myClass() {

    //What goes here?
    var $classNumber = ???//the global $MyNumber;

    private function foo() {
         $privateNumber = $this->classNumber;
    }

}



EDIT: I want to create a variable based on the global $MyNumber but
modified before using it in the methods

something like: var $classNumber = global $MyNumber + 100;

4
  • 9
    The more important question is: Why do you need that? There is probably a design mistake in here.
    – jwueller
    Dec 20, 2010 at 11:24
  • @elusie: I want refer to $MyNumber differently in my classes
    – Zebra
    Dec 20, 2010 at 11:27
  • 1
    Why do you need a global variable in the first place? They are not needed in most cases. What is that global variable for?
    – jwueller
    Dec 20, 2010 at 11:55
  • 1
    PHP Does not have pointers. It does have references, which sometimes work like pointers, but they are not. Do not try to emulate pointer functionality; you will just cause massive pain for future-you trying to debug it.
    – Benubird
    Aug 30, 2013 at 10:38

8 Answers 8

63

You probably don't really want to be doing this, as it's going to be a nightmare to debug, but it seems to be possible. The key is the part where you assign by reference in the constructor.

$GLOBALS = array(
    'MyNumber' => 1
);

class Foo {
    protected $glob;

    public function __construct() {
        global $GLOBALS;
        $this->glob =& $GLOBALS;
    }

    public function getGlob() {
        return $this->glob['MyNumber'];
    }
}

$f = new Foo;

echo $f->getGlob() . "\n";
$GLOBALS['MyNumber'] = 2;
echo $f->getGlob() . "\n";

The output will be

1
2

which indicates that it's being assigned by reference, not value.

As I said, it will be a nightmare to debug, so you really shouldn't do this. Have a read through the wikipedia article on encapsulation; basically, your object should ideally manage its own data and the methods in which that data is modified; even public properties are generally, IMHO, a bad idea.

1
  • 1
    I think the code line "global $GLOBALS;" is useless. $GLOBALS are accessible in the global scope by default. And Warning : As of PHP 8.1.0, write access to the entire $GLOBALS array is no longer supported Dec 7, 2022 at 20:39
15

Try to avoid globals, instead you can use something like this

class myClass() {
 private $myNumber;

 public function setNumber($number) {
  $this->myNumber = $number;
 }
}

Now you can call

$class = new myClass();
$class->setNumber('1234');
7

Simply use the global keyword.

e.g.:

class myClass() {
    private function foo() {
        global $MyNumber;
        ...

$MyNumber will then become accessible (and indeed modifyable) within that method.

However, the use of globals is often frowned upon (they can give off a bad code smell), so you might want to consider using a singleton class to store anything of this nature. (Then again, without knowing more about what you're trying to achieve this might be a very bad idea - a define could well be more useful.)

4

What I've experienced is that you can't assign your global variable to a class variable directly.

class myClass() {

    public $var = $GLOBALS['variable'];

    public function func() {
         var_dump($this->var);
    }
}

With the code right above, you get an error saying "Parse error: syntax error, unexpected '$GLOBALS'"

But if we do something like this,

class myClass() {

    public $var = array();

    public function __construct() {
        $this->var = $GLOBALS['variable'];
    }

    public function func() {
         var_dump($this->var);
    }

}

Our code will work fine.

Where we assign a global variable to a class variable must be inside a function. And I've used constructor function for this.

So, you can access your global variable inside the every function of a class just using $this->var;

2

What about using constructor?

class myClass {

   $myNumber = NULL;

   public function __construct() {
      global myNumber;

      $this->myNumber = &myNumber; 
   }

   public function foo() {
      echo $this->myNumber;
   }

}

Or much better this way (passing the global variable as parameter when inicializin the object - read only)

class myClass {

   $myNumber = NULL;

   public function __construct($myNumber) {
      $this->myNumber = $myNumber; 
   }

   public function foo() {
      echo $this->myNumber;
   }

}
$instance = new myClass($myNumber);
1

If you want to access a property from inside a class you should:

private $classNumber = 8;
1

I found that globals can be used as follows:

Create new class:

class globalObj{

    public function glob(){        
        global $MyNumber;  
        return $this;  
    }
}

So, now the global is an object and can be used in the same way: $this->glob();

-2
class myClass 
{
protected $foo;

public function __construct(&$var)
{
    $this->foo = &$var;
}

public function foo()
{
    return ++$this->foo;
}
}
1
  • 3
    Please use proper formatting and don't post code only, elaborate on your answer.
    – dev0
    Dec 5, 2017 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.