473,473 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

1 New Member
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
6 1320
Dormilich
8,658 Recognized Expert Moderator Expert
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
labmonkey111
44 New Member
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
5,058 Recognized Expert Expert
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
5,058 Recognized Expert Expert
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
labmonkey111
44 New Member
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
8,658 Recognized Expert Moderator Expert
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

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

Similar topics

145
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity...
12
by: williamc | last post by:
Is there anything wrong with having several classes with the same name in the same style sheet? Something like... div.pagedown { margin: 20px 0px 20px 0px; border-top: 1px solid #caa;...
7
by: | last post by:
Hi all I have a class Parent, and two child classes class Child1 and class Child2 derived from Parent class. In Parent class there is a method that will instantiate child classes based on the...
132
by: Kevin Spencer | last post by:
About 2 years ago, and as recently as perhaps 1 year ago, I can recall seeing many posts about what language to use with ASP.Net. The consensus was that employers paid more for C# programmers, and...
6
by: John Rivers | last post by:
hi, here is how to do it and restore sanity to aspx html rendering: (please only reply with sensible architectural discussion - juan) put this at the end of an aspx file (or use an include at...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
15
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting...
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.