I have 2 classes, main and extended. I need to use main vars in extended class.
<?php class Main { public $vars = array(); } $main = new Main; $main->vars['key'] = 'value'; class Extended extends Main { } $other = new Extended; var_dump($other->vars); ?>
Who I can do it?
No valid for example:
<?php class Extended extends Main { function __construct ($main) { foreach ($main as $k => $v) { $this->$k = $v; } } } ?>
I need some solution more transparent and efficient 🙂
EDIT: This can be solved much better with Inversion of Control (IoC) and Dependency Injection (DI). If you use your own framework or one without Dependency Injection Container try League/Container
Answer below left as history of foolish answers.
The correct way I figure.