Say I have the following controller structure:
class Controller {
public function __construct(){
$this->accessControl();
}
}
class Account extends Controller {
public function __construct(User $user){
parent::__construct();
$this->user = $user;
}
}
How to require other devs to explicitly call parent::__construct()
in their child controllers? It contains critical stuff like access control etc
So far I decided to wrap all functions from parent constructor into init()
method which sets initialized
property to TRUE, then check this property in the router. If it's not TRUE - throw an exceptions.
public $initialized = false;
class Controller {
public function __construct(){
$this->init();
}
}
protected function init(){
$this->accessControl();
$this->initialized = true;
}
class Router {
public function process($path){
$controller = new User();
if(!$controller instanceof Controller || !$controller->initialized){
throw new Exception('Error');
}
}
}
Does it smell bad?