Accessing variable from another class

Hi folk,

I’ve been doing procedural php for a number of years now, but just recently I decided to venture into OOP, and it’s a totally different ball game - it’s taking me ages to understand how classes/methods etc work with one another.

For example, is it possible to access a variable set in another completely unrelated class?

I have a method in a class which generates a navigation based on the users privileges, and at the same time - I have a users class which checks the users privileges, name, is a valid user etc.

Now is it possible to access the users class variables from within the navigation class?

For instance:



class users {

public function checkUser($_SESSION) {
//check and pull users data from user database
if ($userrow['admin'] == 1) { $isadmin = true; } else { $isadmin = false; }
}

}

class navigation {

public function createnav() {

//home link
//pages link
if ($isadmin) echo //module link;

}

}


Is this at all possible?

Many thanks in advance!

in your case you probably want to use static variables
example here: http://poundpipe.com/v/baaai/access-static-variables-across-classes

but you might even make things easier by just storing the value in the $_SESSION variable
and creating a static function like User::isAdmin() that checks the session variable and returns true/false

Thank you very much for taking the time out to answer my question, that’s helped me a huge amount, especially your example in the link. :smiley: