PHP class: Global variable as property in class

后端 未结 7 1116
囚心锁ツ
囚心锁ツ 2020-12-05 13:03

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

相关标签:
7条回答
  • 2020-12-05 13:29

    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);
    
    0 讨论(0)
  • 2020-12-05 13:40

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

    private $classNumber = 8;
    
    0 讨论(0)
  • 2020-12-05 13:42

    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.

    0 讨论(0)
  • 2020-12-05 13:42

    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;

    0 讨论(0)
  • 2020-12-05 13:49

    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.)

    0 讨论(0)
  • 2020-12-05 13:52

    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');
    
    0 讨论(0)
提交回复
热议问题