1

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?

4
  • Have you got a singleton pattern in the database? Commented Nov 16, 2015 at 12:00
  • 1
    You are not doing OOP. You just wrap everything in a class which is class oriented programming at best Commented Nov 16, 2015 at 12:00
  • @PeeHaa How can I improve it then to be OOP? Commented Nov 16, 2015 at 12:02
  • 1
    Protip: don't be afraid to type 4 more characters. Terseness often comes at the cost of flexibility. Commented Nov 16, 2015 at 12:19

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for quite frankly the silliest thing I have heard today! And thank you for your input. So are you saying then It would make more sense to lose the Core() class and have the website done procedurally?
"It would make more sense to lose the Core()". Yeah I think so it would be better to have classes with very specific responsibilities. E.g. a routing class, template rendering class, authentication class etc. And you can define them at bootstrap phase either in a normal php file to wire everything together or a bootstrap class which wires everything together.
Some reading material and some watching material for when you get bored :-)
So I've rewritten my entire site structure now to be non OOP, with the objects being things like Database, and now the code is much easier to maintain. Thank you for you're help, My code is a lot more efficient now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.