Stooney Posted March 4, 2008 Share Posted March 4, 2008 I'm having problems with the following error: Fatal error: Undefined class constant 'logged_in' logged_in is a public variable of my auth class. I'm pretty sure I'm going about this wrong but I am trying to call the variable like: $auth=new Auth(); if(Auth::logged_in){ This works on methods just fine (ex Auth::login() ). I've been reading and tried making it static, constant (which wouldn't work out, but I tried anyways) to no avail. Here's an example: Auth Class <?php class Auth{ public $userid=0; public $username='Guest'; public $password=null; public $level=0; public $logged_in=false; public $is_admin=false; public $is_moderator=false; public $error=null; //I left out the methods, they don't matter here } ?> Using it <?php $auth=new Auth(); //Won't bother with passing construct arguments in the example if(Auth::logged_in){ //User is logged in } else{ //Guest } ?> Basically I can't figure out how to access class variables using the scope resolution operator. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 4, 2008 Share Posted March 4, 2008 When ever you use the :: operator, it's accessing the static class.... The class is not initiated or anything when you use ::, just the method is called (or the static variable retrieved). So, $auth and Auth:: are not the same class! You need to use $auth->logged_in. If you're only using this class one time per script, you could declare logged_in as static and access it that way, but that's kind of ghetto. Quote Link to comment Share on other sites More sharing options...
Stooney Posted March 4, 2008 Author Share Posted March 4, 2008 Well I'm trying to avoid globals. My user class checks if the user is logged in (via $auth->logged_in). I could globalize $auth but that's bad. What is the best way for a class to access a variable of another class without globals? (unless of course globals are the right way to go). I do suppose I could pass variables to methods as needed, but is there a better way? I want to avoid one class depending on variables passed from another class as much as possible. It would be easier if one class could just check the 'status' of another class and work accordingly. So this way if one is missing for some reason, everything else could still do it's job. Is this the wrong way to look at it? example: <?php class User{ //username, email, etc function __construct(){ if($auth->logged_in){ //How do I access logged_in without globals? //If the user is logged in set the user class variables } } } class Auth{ public $logged_in=false; //methods omitted } //Usage $auth=new Auth(); //assume auth is set up and the user is logged in $user=new User(); ?> Quote Link to comment Share on other sites More sharing options...
keeB Posted March 4, 2008 Share Posted March 4, 2008 This has nothing to do with globals. You use :: outside of a class when it's a static object. Your second code example is wrong. It should be the following: <?php $auth=new Auth(); //Won't bother with passing construct arguments in the example if($auth->logged_in){ //User is logged in } else{ //Guest } ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted March 4, 2008 Share Posted March 4, 2008 I see what Chris wants to do. I think you probably want something a little like this: <?php class User{ //username, email, etc function __construct(Auth $auth){ if($auth->logged_in){ //How do I access logged_in without globals? //If the user is logged in set the user class variables } } } class Auth{ public $logged_in=false; //methods omitted } //Usage $auth=new Auth(); //assume auth is set up $user=new User($auth); ?> i.e. passing the auth class to the user. HOWEVER, i think you're more likely to do it the other way around. In other words you have a user, who wants to be authenticated. Thus you have an auth class, and you have a method (say authenticate) which takes a parameter of type user, and then uses the user information to. class Auth { public function authenticate(User $user){ // get $user->name // check if name is valid // return true (i.e. authenticated). } } class User { public $name; } $user = new User(); $user->name = "Chris"; $auth = new Auth(); if($auth->authenticate($user)){ echo "Yes we authenticated"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.