I have a class, called Core() That pretty much runs the management of my website. It handles the routing, the header, body, footer etc and all files/database/authentication (login/logout) access.
So far it works great. Pretty much All I have to do in my index.php is the following:
$core = new Core();
$core->render();
And the correct page will be rendered.
My problem is when it comes to databases. I currently have my PDO database classes used by Core() as a trait, loaded from a separate file.
So in Core::__construct I have to manually start (what would be) the __construct function for the database:
$this->database_initialize();
Which connects to the database using the given credentials.
The problem I have is that sometimes I need to use other classes, and they also need access to the database. Right now, if I need to run a select statement on one of the pages of the website I simply do the following:
$this->select("SELECT foo FROM bar;");
This is nice and easy, and part of my reasoning for using the Database(); as a trait - Otherwise I would have to write $this->db->select("SELECTfooFROMbar"); and I don't like typing out silly variables all the time. (Before I moved over to the whole OOP system, I was just typing $table = select(""); which was nice and quick.)
So let's say I want to load a class on one of my pages, I would do the following:
$form = New Form();
That's fine, but how do I get that class to access the $core database that's already been loaded? Because inside the class Form I cannot write:
$data = $this->select("");
Because the class Form doesn't have a select() function. Within the Form class, I also tried $data = $core->select("") but it has no idea who or what $core is either.
Little help?