TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-20-2008, 12:32 AM   #1 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default Your oppinion counts

Howdy everybody. So, I have a DAL system setup, right? I need your thoughts about its implementation, and whether you would want to modify the factory or the interface if you were to develop an Oracle driver for instance - or any other database driver - (it's all about extensibility):

- Interface:

PHP Code:
interface DB_Driver
{
    public static function 
getInstance();
    public function 
connect();
    public function 
select( array $a );
    public function 
insert( array $a );
    public function 
update( array $a );
    public function 
delete( array $a );

- Factory:

PHP Code:
final class DB
{
    public static function 
getInstance$type 'mysql' )
    {
        
// require the database driver
        
self::$instance =& mysql_driver::getInstance();
    }

- and, of course, the mysql driver (the default one):

PHP Code:
class mysql_driver implements DB_Driver
{
    
// singleton, implements the interface methods and its own methods (if you need anything, just write it :)

Only some excerpts are shown here, the real code doesn't matter that much. The idea itself is more important than the actual code.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.

Last edited by xenon : 01-20-2008 at 04:52 AM.
xenon is offline  
Reply With Quote
Old 01-20-2008, 01:35 AM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

The question I'd have to ask is with the abundence of high-quality tested database abstraction libraries about nowadays (including PDO built into PHP itself), is it worth rolling your own?

But, if you do want to roll you own, my suggestion would be to use one based on ActiveRecord. For an implementation example, take a look at CakePHP.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-20-2008, 04:51 AM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Yes, it is worth. Why? Simply because this system will be part of a proprietary system, but I want to provide a gate for developers to write their own drivers if they want/need to use some other database such as Oracle or SQLite or Postgre, etc. I've thought of the Active Record pattern before writing the actual system and I've decided that an array-based library is faster and less memory consuming than an object-based one (it's enough that the whole system is almost fully object oriented).

So why don't I use some free software (like an ORM or any other library from the internet), you would ask. Simply because people will pay for this software. And free software doesn't really work in a proprietary system. I use my own license, I have taken time to create the software, and I'm not risking getting sued over some stupid library (and this is very interpretable, believe me). The only thing that concerns me right now is that the system will be extensible - so if a developer wants to take a component or a library and plug it into the system, he can do just that without having to change the default libraries - which will be updated from time to time.

So, this is some kind of a review I need from other 'colleagues'
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-20-2008, 10:29 AM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Ok, each to their own

One more thing to add to my original post though for the benifit of anyone else who is thinking of writing their own database class, if you are worried about speed, use PDO, it's much faster than a function wrapper class and provides support for every database system that PHP supports nativly. Also, the major downside of using a function wrapper system is that if you want to extend it to support other database servers in the future, you're stuck with writing all your queries in SQL92 which means you loose a lot of functionality such LIMIT.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-21-2008, 10:14 AM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

I remember writing an article ages ago about this, ahh the good old days.

looks good to me m8, although you dont need to tell php to get a reference:
PHP Code:
self::$instance =& mysql_driver::getInstance(); 
as php5 passes objects around by ref automatically and infact (i believe) your code may produce a E_STRICT error (or is that just for the 'new' keyword? not sure on that), so
PHP Code:
self::$instance mysql_driver::getInstance(); 
would achive the same ends.

apart from that all is good.

i also like to create my own DAL, gives me more control and is less bulky and cumbersome as others.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 01-21-2008, 11:40 AM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

PHP might pass objects by reference automatically, but when I need the reference to a variable, I explicitly reference them, to make it more clear to me that I'm using a reference and not the object itself. And yes, this snippet:

Code:
$some_obj =& new TestClass();
would leave you with a fatal error and nothing more (because the object is just being created, it doesn't yet have a reference until the script steps through that line). My implementation doesn't throw any errors/warnings/notices, because the object is referenced correctly and exists in memory at that point.

Thanks for the feedback. So the system will stay
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-21-2008, 12:28 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Just a couple of points on your code, xenon.

Firstly, your code snippets in the first post don't show you declaring the $instance static member for the DB factory. Failing to declare that will result in a fatal error when you try to assign it the driver instance later on. Since you stated that your implementation doesn't throw any errors then I can only assume that you deleted it from your code sample in the first post to keep things short.

A useful addition for 'extensibility' as you put it, would be to make use of that argument for the DB::getInstance method. For example:
PHP Code:
if (class_exists($type.'_driver'TRUE))
{
    
// assign instance of driver

So if the class isn't available, and cannot be autoloaded, you can throw a relevant exception or fall back on something else should you choose. Someone might decide that it's fun to call DB::getInstance('wobblybobblydb') which you don't have a driver for. At the moment it is hard-coded to use the MySQL driver which I'm sure you'll agree isn't particularly extensible.

Another point is that sketch was right about your driver instantiation producing an E_STRICT warning. The associated message is, "Only variables should be assigned by reference". The simple solution, unless you don't mind bending the strict standards from time to time, is just to not use the & and let PHP do its own job of passing around references to objects.

I think there might have been another point or two that I wanted to post about but the urge to find some lunch is taking over so I can't remember what those things were! As such, I'll leave things here.
Salathe is offline  
Reply With Quote
Old 01-21-2008, 01:01 PM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
$some_obj =& new TestClass(); would leave you with a fatal error and nothing more
are you sure? this was standard procedure in php4, because the php4 object model didnt pass objects by ref, it made a copy, thus
PHP Code:
$pObj = new lolItsAClass(); 
means that there is a copy of the object being made, so to combat we have to tell the php interpreter that we want the reference and not the copy:
PHP Code:
$pObj =& new lolItsAClass(); 
in php5 this was updated so that the 'new' keyword automatically returned a reference to the newly created object and not a copy, hence the need for the 'clone' keyword (to replicate objects), i cant test my thorey because im at work.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:38 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design