I am setting up variable values in a constructor function.
To keep everything organised, I have created other classes which will only be instantiated once in the "application" class.
I want to pass protected variables value to other classes(frotend, backend ...). I know we can create same variables in these classes & pass variables as arguments. This will lead to a lot of code repetition.
Is there any better way around?
Thanks
class application{
protected $name;
protected $version;
protected $slug;
public function __construct(){
$this->name = $name;
$this->version = $version;
$this->slug = $slug;
$this->includes();
}
public function create_settings(){
//Only one instantiation
$frontend = new Frontend_Settings();
$backend = new Backend_Settings;
//.. more like these
}
}
class Frontend_Settings{
public function __construct(){
print_r($name.$version.$slug);
}
}
class Backend_Settings{
public function __construct(){
print_r($name.$version.$slug);
}
}
$firstapp = new application( 'First app', '1.0', 'first-app');
$secondapp = new application( 'Second app', '1.0', 'second-app');
$frontend = new FrontEnd_Settings($this);Then (assuming app has getter functions), frontend has access to all the variables of Application:class Frontend_Settings { public function __construct($app) { printf(“%s.%s.%s”, $app->name(), $app->version(), $app->slug() ); }}where name() et al are getters.