I have created a user class that has information about the user. Also, I have extended that class with another class called LoggedInUser.
The point of LoggedInUser is to be able to easily tell if the user is authenticated and logged in by simply checking for the existence of the LoggedInUser object like this:
class User {
//properties
}
class LoggedInUser extends User {
//some additional token stuff
}
$isLoggedIn = new LoggedInUser();
class Router {
public function gotoRoute() {
if($isLoggedIn) {
// perform the action
}
}
}
As far as I can tell, $isLoggedIn is not accessible to the class, so should I pass it into the constructor? or is there a better way to do all of this?
In my opinion you have two options. You can pass the
LoggedInUserinto theRouter::__construct(). Or you can create theLoggedInUserin theRouter::__construct()1st option:
2nd option
I like the 1st option. It is called Dependency Injection