Reputation: 5007
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("SELECT
fooFROM
bar");
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?
Upvotes: 1
Views: 85
Reputation: 72652
For starters having a class called Core
is a huge code smell. Which is also made clear by your next sentence:
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.
This means you basically have a single class which is responsible everything and then some. Which means you gain nothing by wrapping it in a class.
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.
What's the reason for using a trait (for this)?
Otherwise I would have to write $this->db->select("SELECT foo FROM bar"); and I don't like typing out silly variables all the time
That's quite frankly the silliest thing I have heard today and a terrible reason to go one route or another.
If something needs a database connection pass it in:
$dbConnection = new \PDO('{dsn}', 'user', 'pass');
$somethingThatNeedsDb = new SomethingThatNeedsDb($dbConnection);
That's fine, but how do I get that class to access the $core database that's already been loaded?
You don't. You pass it also in the form that needs it:
$dbConnection = new \PDO('{dsn}', 'user', 'pass');
$theForm = new Form($dbConnection);
$somethingThatNeedsDb = new SomethingThatNeedsDb($dbConnection);
Upvotes: 4