By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

Making PHP Classes Play Nicely With Each Other (OOP Pattern Question)

P: 1
Hi all,

I have a PHP OOP design question. I've done some searching in google, but wasn't able to turn up anything concrete. I'm hoping some experts can point me to the most elegant solution.

I have a set of classes. All of my classes "extend" an abstract class called Base. (This class reads a config file, has a basic set of common methods, and sets up needed parameters)

Most of my classes need a class called Mysql to perform database operations.

Is the best way to access the Mysql class to pass it in the __constructor in each of my child classes, or to instantiate it in my Base class, and assign it to a public property? Or perhaps there is a better way altogether?

I'm really looking for the best way to do this, so thanks in advance for your advice!
Oct 13 '08 #1
Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
One common way to incorporate a db class is using the singleton pattern (though there are others like the registry pattern). in this case you query the class with the class call (e.g. mysql::connect() (connect to the db (will do nothing if connection is already established); mysql::query($query))

regards
Oct 13 '08 #2

P: 44
I think it would be a much better design to have it in the base class. The whole point of inheritance is to cut down on code duplication and share code among classes. But I would recommend making the Mysql object protected in the base class. That way only that class and all subclasses can access it. Its a little more secure that way.
Oct 14 '08 #3

Atli
Expert 5K+
P: 5,058
Hi.

What I usually did was to create an instance of my database class and pass it into the classes that need it. (usually via the constructor).
This way there is only one instance of the class in play.

Another method I have used in the past was to have the class extend my database class, which would allow me to use the database class as a part of the current class.
This is obviously not a good idea if you use lots of objects in your code, but can be made to work well in smaller projects.

Then you could always use a static approach, creating a static class that could be used everywhere without actually creating an object instance to pass around.
This is probably the simplest way, but for some reason not that appealing. (Kind of feels like abusing the global scope :P)

A compromise between the two could be to create a static class that creates or returns an existing database object. That way you would only have a single instance of your object in play, without having to pass it around. It could simply be fetched wherever it is needed.

Somewhat like:
Expand|Select|Wrap|Line Numbers
  1. class MyDatabase {
  2.     private static $_link;
  3.     public static function GetConnection(){
  4.         if(self::$_link == null){
  5.             self::$_link = new mysqli("host", "usr", "paswd", "db");
  6.             if(mysqli_connect_errno()) {
  7.                 throw new Exception("Database connection failed: ". mysqli_connect_error());
  8.             }
  9.         }
  10.         return self::$_link;
  11.     }
  12. }
  13.  
  14. class foo {
  15.     public function DoSomething(){
  16.         $dbLink = MyDatabase::GetConnection();
  17.         $result = $dbLink->query("SHOW TABLES");
  18.         if($result)    {
  19.             while($row = $result->fetch_array()) {
  20.                 echo " - " . $row[0] ."<br />";
  21.             }
  22.         }
  23.         $result->close();
  24.     }
  25. }
  26.  
This is my favorite at the moment :)
Oct 14 '08 #4

Atli
Expert 5K+
P: 5,058
I think it would be a much better design to have it in the base class. The whole point of inheritance is to cut down on code duplication and share code among classes. But I would recommend making the Mysql object protected in the base class. That way only that class and all subclasses can access it. Its a little more secure that way.
A good point, but...

If you have your base class initialize a new instance of the database class for every new class, then you may end up with a bunch of open database connections for each execution of your code, which could affect performance.

A few lines of repeated code would be better than that, wouldn't you agree?
Oct 14 '08 #5

P: 44
A few lines of repeated code would be better than that, wouldn't you agree?
Yes, that sounds a lot better. I was thinking purely in terms of OOP, obviously databases change things a little.
Oct 14 '08 #6

Dormilich
Expert Mod 5K+
P: 8,639
This is my favorite at the moment
as I said, a Singleton.

If you have the PEAR package installed, you can also use one of PEAR's db classes.

regards
Oct 14 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.