php - Why can't I access this global variable? -


i of course tried out $globals , still no go. syntax correct. understanding $db_user in global scope.

<?php     $db_user = 'foo';     class database     {         // not work         private $db_user =              $globals['db_user'];         private $db_pass =              'foob';         private $db_driver =            'foob_foob';         // ... 

you calling $db_user inside of class method, means calling variable local scope (within class). fix this, tell php you're looking global variable adding global $db_user inside of methods used (or use constructor add class scope):

class database {       private $db_user =              '';     private $db_pass =              'foob';     private $db_driver =            'foob_foob';     // snip      // method 1: add variable class scope constructor            public function __construct()     {       global $db_user;       $this->db_user = $db_user;    }      // method 2: tell php want global variable in methods    public function foo()     {       $global $db_user;       ...     } 

with method 1, can call $this->db_user instead of $globals['db_user'].

with method 2, add global $db_user each, , use $db_user.

for more information see http://php.net/manual/en/language.variables.scope.php


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -