开发者

How to pass Global variables to classes in PHP?

开发者 https://www.devze.com 2023-02-06 07:34 出处:网络
How can I pass the global variables to the classes I want to use without declare them as GLOBAL in every method of the class ?

How can I pass the global variables to the classes I want to use without declare them as GLOBAL in every method of the class ?

example :

$admin = "admin_area";

if($_POST['login']){
      $user = new Administrator;
      $user->auth($pass,$username);
    }

in the class Administrator I want the variable $admin be accessible in all methods of the class without doing this 开发者_如何学C:

class Administrator{

   public function auth($pass,$user){

     global $admin;
     .....

  }
  public function logout(){
    global $admin;

    .....
}

} 


Well, you can declare a member variable, and pass it to the constructor:

class Administrator {
    protected $admin = '';

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

    public function logout() {
        $this->admin; //That's how you access it
    }
}

Then instantiate by:

$adminObject = new Administrator($admin);

But I'd suggest trying to avoid the global variables as much as possible. They will make your code much harder to read and debug. By doing something like above (passing it to the constructor of a class), you improve it greatly...


Generally, PHP provides $GLOBAL array, which can be used to access superglobal variables. BTW, using this array is not much better than using "global" statement in the beginning of all your methods. Also, using such approach does not seem to be a good practice for object-oriented code.

So, you can:

  • either pass these variables as constructor parameter (see answer from ircmaxell for details)

  • or use some configuration class (singleton)

Decision depends on semantics of your global parameters.


Copy them into object variables in the constructor.

 global $user;
 $this->user = $user;

It's not too elegant, anyway, using globals is not too elegant.


Try passing the variable that you want to be global to the object as an argument, and then use the __constructor() method to make it a class property.

You can find more info and examples here: In php when initializing a class how would one pass a variable to that class to be used in its functions?


The best way to achieve this is a singleton, in my opinion.

The singleton is a class with a static property and a static method that you will use to call the object from everywhere, 'cause static methods can be called everywhere in the code.

Example:

class Singleton 
{
   private static $instance = null;

   private function __construct()
   {
     //...whatever...
   }

   private function __clone()
   {
     throw new Exception('Not clonable..');
   }

   public static function getInstance()
   {
      if (static::$instance === null) {
         static::$instance = new Singleton();
      }
      return static::$instance;
   }
}

Now to call it from wherever you want you can use:

$instance = Singleton::getInstance();
0

精彩评论

暂无评论...
验证码 换一张
取 消