I'm new to OOP so there's probably something fundamental I'm missing here. In the Foo
class, the $this->user_group
variable is not available to the bar()
method. Why is that? And is there a less messy way to get these classes to all talk to each other than including the $DB
(and others) every instantiation (this gets hairier with my actual code, which has many more classes). Here's an example:
class Foo {
private $Auth, $user_group;
function __construct($DB) {
$this->Auth = new Auth($DB);
$this->user_group = $this->Auth->get_user_permissions_group();
// ("echo $this->user_group;" here will output the correct value)
}
public function bar() {
// ("echo $this->user_group;" here will output nothing)
return ($this->user_group > 1 ? 'cool!' : 'not cool!');
}
}
class Auth {
private $DB;
function __construct($DB) {
$this->DB = $DB;
}
public function get_user_permissions_group() {
$result = $this->DB->query('return user permissions level from DB');
return $result; // int, 1-3
}
}
$DB = new Database();
$Foo = new Foo($DB);
echo $Foo->bar();