0

I am pretty new to OOP and so this question is more to do with theory, than my actual code. I don't have access to my code at the moment but will add it soon.

I have a 'database' class which allows me to create new PDO connections and interact with various databases. I pass the database connection details when creating an object, and the __construct method builds the PDO connection with these values.

Then I have various methods for interacting with the database e.g prepare, execute etc

I created an object for each connection that I need, and this allows me to quickly query any of my databases by just calling the object and the method I want to use.

I am looking to create another class called 'users' which will allow me to interact with the users table. I would like methods to add, remove & modify user details. I was hoping to use an existing object to query the database within each of these methods. Rather than having to create a new object and pass it the connection details (So that I only have to update my connection details where the original object was created, rather than updating it in multiple places.

It seems that I cant do this because my 'database' object is outside of the scope of my 'users' class.

Can anyone advise me how I can make this object available within the 'users' class? More importantly is this the wrong approach and is there a better practice?

3 Answers3

0

You could look at making your database class static (so it can be accessed from 'everywhere' using Database::Query() etc.

However in this instance it's more appropriate to have a singleton, you will create one instance of your database class and use this in all your objects.

Google singletons in PHP for a guide, but a common pattern is to have a static method on the database to GetInstance() which will create a new instance of the Database it one doesn't already exist.

Pez Cuckow
  • 13,014
  • 15
  • 75
  • 121
  • Singletons are almost always a bad pick: just create an instance of the `Database` class, and inject it into right place [via constructor or setter either]. – moonwave99 Jan 08 '14 at 15:31
  • @moonwave99 why would it be a bad pick? He will only ever want one instance of the database and using a singleton ensures that? – Pez Cuckow Jan 08 '14 at 15:34
0

Simple pass your database object as a param of the specific userclass object:

$userobject->createUser($databaseobject);

or pass it when you create the user object:

$userobject = new User($databaseobject);

xQbert
  • 31,937
  • 2
  • 37
  • 57
niyou
  • 869
  • 1
  • 11
  • 21
0

You should only need one PDO object per database.

Unless you plan on doing pretty hot stuff, I suspect you will never need more than one database (with multiple tables). Creating a generic layer of multible DB handling seems a bit of an overkill to me.

What you seem to be doing right now is to create a DB connection each time you want to access whatever part of the same database.

Once you get a PDO DB handle, you can use it to access any part of your DB. Each time you instantiate a PDO object, you create a new connection to the same DB.

Since the number of simultaneous DB connections is limited, you're eating up costly and rare resources for no good reason.

So my advice would be to make the PDO object a static class variable, and build whatever accessor class by inheriting a single instance of the same PDO object.

kuroi neko
  • 7,721
  • 1
  • 16
  • 38
  • The site that I am looking to build will require access to various databases. I was hoping to create a class containing methods to utilise PDO. This way I can simply create a new object and pass the connection details when adding a new database. I was hoping to then call the required object when building methods in other classes, if that makes sense? – user3173706 Jan 08 '14 at 16:14
  • @user3173706 Since you are in control of the database(s), I really don't see the point in creating more than one. It would be very costly. Multiple tables is the usual way to go. Or do you have a good reason for doing otherwise? – kuroi neko Jan 08 '14 at 16:18