php - Checking for class instance within another class -
i have created user class has information user. also, have extended class class called loggedinuser.
the point of loggedinuser able tell if user authenticated , logged in checking existence of loggedinuser object this:
class user { //properties } class loggedinuser extends user { //some additional token stuff } $isloggedin = new loggedinuser(); class router { public function gotoroute() { if($isloggedin) { // perform action } } }
as far can tell, $isloggedin not accessible class, should pass constructor? or there better way of this?
in opinion have 2 options. can pass loggedinuser
router::__construct()
. or can create loggedinuser
in router::__construct()
1st option:
$loggedinuser = new loggedinuser(); $router = new router($loggedinuser);
2nd option
$router = new router(); // in router class file __construct() { $this->yourproperty = new loggedinuser(); }
i 1st option. called dependency injection
Comments
Post a Comment