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 :
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:
Then instantiate by:
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…