How to pass Global variables to classes in PHP?

后端 未结 5 1584
终归单人心
终归单人心 2021-01-05 02:41

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 = \"         


        
5条回答
  •  日久生厌
    2021-01-05 02:57

    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.

提交回复
热议问题