php - Inheritance: how to properly set global vars in OOP architecture without using $GLOBALS -
i'm building mvc framework scratch fun , learning purpose.
what i'd have conf file configuration params (host, default language, current language, etc.) , have params available in every script of framework without using $globals. it's couple of month i'm oop so, make simpler, idea has been create class private properties, create protected function env() subclasses can retrieve properties , put class @ top level in hierarchy letting other class extending or other subclasses.
class environment { private $full_url = "https://www.example.com"; private $relative_url = "test"; private $default_lang = "en"; private $current_lang; private $admin_area = "admin"; private $user_area = "user"; function __construct() { $this->setcurrentlang(); } private function setcurrentlang() { $this->current_lang = "it"; } protected function env($var) { if (isset($this->$var)) { return $this->$var; } else { echo "environment variable <strong><em>$var</em></strong> not found in <strong>" . __file__ . "</strong> on line <strong>" . __line__ . "</strong>." . php_eol; } } } this method worked until set __construct() method in subclasses also. problem subclasses need independent constructors but, if don't call parent::construct(), environment class' constructor not called , param $current_lang not set.
this bad design want achieve so, keeping in mind goal, have suggestion?
to thorough, general designed planned one:
environmenttop classrouter extends environment(router receives requests , set var $request should used other sub classes)- in
__construct()routerloads , instantiatescontroller, extendsrouter - then controllers,
examplecontroller,extend controller - the same happens
model,extends router, , subclasses e.g.examplemodel extends model
at point, how can retrieve private params contained in environment set inside contstructor without calling parent::construct() in each sub-class?
i hope explained problem.
Comments
Post a Comment