I am going to try to explain this as best as I can, forgive me if parts don't make total sense. I am building a PHP library/framework for educational purposes. I was building it using the singleton pattern but ran into some problems and also seem to read lately that it shouldn't be used as it isn't very testable, (and although I am not worried about this being testable, I would like to learn how to do it) and instead should use dependency injection.
What I want is a shared variable, if you will, which houses many different classes. For instance $app->database; $app->views; $app->session; etc., and am not sure if I am doing this right. All classes are also being autoloaded.
I have the bootstrap/start.php which initializes the main variable (is there a name for this?) with:
$app = new App(new Database, new Views, new Session);
in the App.php class I am using essentially:
public $database;
public $views;
public $session
public function __construct(Client $client, Views $views, Session $session)
{
$this->database = $database;
$this->views = $views;
$this->session = $session;
}
My problem is this: Am I supposed to be initializing all of the classes in the constructor like that? And the big one...how do I use the $database variable in, for example, my Views.php class without creating a whole new instance which may overwrite some of the properties in the Database class?
Would appreciate some insight on this a lot.