![]() |
To abstract, or not to...
So I just ran into another snag. I've been developing a small group of classes that all use mysqli. After two weeks of work on my local test server, I uploaded them to my host to put em online to show off, and.... my host doesn't have mysqli, it's mysql or bust.
Not only do I hate them because for such a large company they barely offer more than a skeleton server, but I also hate them because they brought it to my attention that my scripts aren't very backwards compatible at all. So now I want to create a very very basic abstraction. I'm thinking two classes, both sadly just reproducing the same methods the mysqli object uses, but one which would use mysql functions if that extension isn't loaded on the server. At first I thought this would be a good chance to play with abstract classes, but I dunno. Any ideas for how to go about this? I basically want something in the background to check function_exists('mysqli') and use one of two possible sets of methods based on that knowledge. Maybe it'd just be easier to release two versions of this entire thing, one based on mysqli, one on mysql heh... -m |
The approach I would take would be something like the following. First I would create an interface to hold all the child class methods that I'll be needing for my database class:
php Code:
Then I would create the child classes, one for each single database type possible, which all implement that interface: php Code:
And finally add the database base which would be the class that's first initialised: php Code:
Then last of all we would initialise that class we've just created, and set the database we want to use by using the setDatabase function. By doing that, we can seamlessly change database types, as the majority of the code is kept in the child classes.php Code:
As we have set the database type to PostgreSQL, any function calls will be routed to the PostgreSQL child class. Voila! |
That is definitely one of the most in depth answers I've gotten to one of my random scenarios, thank you.
I think I cornered myself away from the answer when the idea of an abstract class first struck me... I was pretty close to that answer, it just didn't seem right in my head, because I was picturing a relationship between two types of classes (the abstract, and it's children). It seemed like it should work, but I just kept snagging on some unknown feeling of not being quite right. Having an abstract/interface, children and a controller suddenly makes all the queasiness go away and that little light go *bing*. -m |
Pleasure :-) !
In theory though you could go down the abstract path, with something like so:php Code:
However, the reason I would take the former path, as aforesaid in my earlier post, is due to the logging and other centralised functionality that would be beneficial to the overall harmony of a database class. Whenever you make a call in the former, you can perform any generic tasks, such as logging, whereas if you take the latter route, with abstracts, you'll have to set-up logging in each individual child class, or at least call a function from the abstract. |
TalkPHP - Using Abstract Classes to Define Rules
TalkPHP - Working with Interfaces may help. An interface is a prototype/blueprint of an implemented class, in otherwords it defines rules that a class must implement and nothing else and they cant contain functioning code. An abstract is like an interface but you can define some of it to be functional code that will be available to the derived class through inheritance. So in short, abstracts are much like interfaces, but they allow you to have common methods inherited into derived objects, where as interfaces just enforce the API of the object. |
Thanks SketchMedia.Today I was trying to learn Abstract class and methods from a book. I downloaded the from their website. It was giving me Error.
By read your Article.The error is solved And Saved me lots of time. |
| All times are GMT. The time now is 01:03 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0