PDO - Why it should not be part of core PHP!

Published 2004-10-22 18:13:42

Ever since PDO was proposed, there have been warm fuzzy feeling about it's iminent arrival. I hate to say, though I fear it could be the biggest mistake PHP ever makes.! (well I guess among the worse).

That's not a lightweight statement, and I'll fire of the disclaimers, before I go any further.

I'm working on an alternative at present.

I know the authors of PDO personally, and I probably owe them a drink for dissing the work.

From what I've seen of the code in PDO, it's pretty damn good.

So what you may ask is so wrong with it?

The major issues I have with PDO are

Reuse, recycle...

When there was at least 2 capable, cross platform database libraries available. Rather than examine if they where a feasible option, it was decided to go off and start from scratch.

Having spent quite a time working with libgda, probably the best library available to do this, It has a 5 year head start, fully tested, and is considerably more feature rich than PDO will ever be.

PDO seems to be slowly gaining backends, however, It's a long way from having as many as libgda. And for each of the features missing in PDO, It will take time to ensure they are enabled in all the backends, let alone fully tested.

Copying C is not always a good idea..

The worst features of PDO is variable binding (similar to pointers). This is what could be regarded as 'minefield programming'. In some of the worst wars in history, army's laid mines all over stratigic borders, so that the oposing army would be delayed in crossing. What happened after the war was a tragic travasty, the abandoned minefields started injuring innocent civilians.

The way variable binding is done in PDO mimics this behaviour, while it achieves the goal of making updating rows relatively simple, It has the significant downside that it makes code unpredictable, splattering variables, which if changed may update a database, without making it clear and simple that this may occur. This whole issue should have been dealt with by using objects to represent database rows, and only make these usable as 'updateables' (see the introduction to DataObjects) which is exactly how DBDO was designed.

Cool features dont always make great features.

Iterators where introduced with much fanfare in PHP5, using foreach on an object that implements the iterator methods, and result in a fetch operation on every foreach. The simple example below illustrates the crux of the problem. (and I've already seen people pastebin iterator code, and you really are making wild guess as to the intent of the code)

Without sensible variable names, this rather confusing code easily evolves into complete jiberish..

While I believe you can still use PDO without using iterators, They do lie at the heart of the problem. I dont know if it was an off-the-cuf remark, but It appears that PDO started as a proof of concept for using iterators on Databases, and as it evolved became a limited Database abstraction layer, which leads into the last issue.

Design by evolution, at the beginning or end.

I dont think I've ever come away and designed an interface correctly the first time, by now DBDO has a long history.

DBDO takes DataObjects as a base, removes alot of cruft that has built up, and at the same time introduces extensive datatype support, lazy fetching.

This now leaves DBDO at a stage where most of the documenation is already done (as part of DataObjects).

In comparison, since PDO does not appear to have a history, It's first generation API has not had a chance to be picked apart by thousands of users. It lacks documenation (although that apparently will be fixed soon.)

I'm sure there are some valid reasons why DBDO could not stand up as the next generation Database library for PHP5, but I wonder seriously if those are less significant than PDO's.

Win32 SupportAfter considerable work over the last month, this is pretty much solved. (Although I only bothered building mysql/postgres for win32, to continue the process should be pretty simple.). Given this work already, the amount of effort to make it 'enterprise ready', is pretty minimal now.

Too many libraries?Libgda has a midsized dependancy list, although this is not that major compared to libxml2, which is now part of the standard distribution. Currently DBDO needs

libgda (obviously)

libglib

libgmodule

libgobject

libgthread (maybe not necessary)

libxml2 (used by PHP's xml extension already)

libxlst (used by PHP's xlst extension already)

iconv (used by PHP already)

Not sure if this blog will change the fact, but by not using libgda, I think PHP is missing a huge opportunity to stand on the shoulders of giants, and get a world class Database backend, with a limited effort.

Comments

- reuse/recycle: on the whole, I agree. In the specific case of libgda, IMO, the dependency list is a bit too heavy considering that most of the features provided by those dependencies already have a PHP internals equivalent. In a sense, we're not really re-using that infrastructure with libgda, but duplicating it.

- copying C: I agree that bound parameters can lead to magic code. There are two things to keep in mind: a good coding style (with comments) is the best remedy for code that might be considered magic.
And secondly, the use of bound parameters is optional, and there is a non-magic variation if prefer.

- iterators: they aren't even implemented yet, and they certainly are *not* the core feature of PDO. Again, their use, once implemented, will be optional.

PDO is certainly young, but it is designed to work with the PHP internals, rather than around them, which is something I view as a major plus point for PDO, and a bit of a minus point against any other existing db library.

My opinion on libgda is that the dependency list is a bit high, and the problems you mentioned with win32 have me concerned that it will be painful to install on *nix (note that I've never seen a system with it installed, and couldn't name an application that uses libgda), and perhaps not be functional on win32. I feel that this is enough of a reason to not have an extension that uses libgda in the core distro.

On that topic, I personally don't mind if PDO goes into the core or not, and to be honest, I would be happy to have PDO stay in PECL, as it will make it easier to upgrade outside of the regular PHP release cycle. This same idea applies to any/all PHP extensions IMO.

Well, although I tend to agree with the bound magic thing... It's the argument of giving power to the experienced users vs. preventing newbie suicide. I mean if you think people would shoot themselves with goto, how much do you think they'll shoot themselves with this??

I think interators are VERY useful...but not the way you show them used

why would you want to iterate the connection? why are you storing the result with the connection? you'd want to iterate the result

Why would it need to be either or? From my perspective, DBDO will be great when I want to have database abstraction (software I may distribute and want to gain adoption). However, in my work at my day job I am not really looking for abstraction; PDO seems to provide consistency without abstraction.

I think alan has one good point. When should we wrap a third party library (thereby accepting a number of deps) and when should we write from scratch. How many addition LOC are we talking here? How happy are you with your windows solution? Will the experience we different for windows users (performance, stability ..)?

His other points go back into the discussion of how much should we protect the unwashed masses form themselves. For iterators we have already made a decision. For variable binding we havent specifically, but I dont see this as a biggy.

You have some valid points. While I do agree that the maturity of preexisting solutions is desireable, I do not agree that PDO needs anything more than what it already has.

Your criticisms of data binding and pointers, however, pisses me off. Personally, I find goto statements to be proof of Satan's existence. How many coders actually use goto statements except when playing a practical joke? They aren't a problem. Even if they were, I still think goto statements should be in every language.

Pointers exist in C because there is no other way in such a low-level language. PHP is higher level and offers a multitude of features in the language itself. Pointers aren't necessary, but I want them anyway. I want every available language construct to be at my disposal. Good and bad.

I'm tired of puritanical language nazis telling me what I can and cannot use. I like pointers. They don't give me problems. If people can't grasp them, don't use them. Provide warnings. DO NOT dumb down a language because the masses are dumb.

My issue with PDO is completely different from yours. What use is another database abstraction layer? I know that the developers say it is not another DB abstraction layer like pear DB, but it is! And it doesn't prevent your from using implmentation-specific SQL! (For example, with PDO, I can still use MySQL's DATE_FORMAT and INTERVAL and what not).

What PHP *REALLY* needs, IMO, is a data access object interface. There are some pretty smart developers around the PHP circle, and I'm sure that they could tackle the problem of writing a generic data access module (like the Pear::DB_DataObject project) to be integrated into PHP core. This would be FAR FAR FAR more useful than another abstraction layer like Pear::DB.

In reply to Taylor about the fact that PDO is just another abstration layer, he got it right. But isn't that always the case in PHP ? First you would program a function in PHP yourself, then you release the code, then everybody use it then it becomes a part of the core code. I don't think the PHP team was trying to bypass PEAR::DB but re-use the good idea and adapt it to the new features available in PHP5. When you code enterprise level application, you need a flexible solution. The error handling in PEAR::DB was not very good, neither is the one from DataObject ... Integrating the exception mechanism is one really good reason to switch, although I know a lot of you guys would do a die(mysql_error()) in the middle of your code, which is not possible on large scaled, security needy projects.

Another great advantage is that PDO is coded in c, not in PHP, which makes it much faster than a PHP framework.

$myarray = array();
// Code to initialize $myarray
foreach($myarray as $value)
//Can you tell what $value is ??? Nop
}
this issue is inherant to the PHP language, not PDO.

+ you can always choose the implementation you want.

PHP must continue to grow in that direction and become more and more like other prefered entreprise language like Java. I congratulate the PHP team for the fantastic work they are doing, keep on moving up !

Well... It is nearly 4(!) years from first comment for this article. We use oci8 extension at this time and we still use oci8 now... The one of the reasons is that PDO_OCI does not support PDO::PARAM_STMT as IN OUT bind variable in any driver and we havily use stored procedures with recordsets. So PDO was and still useless toy in our case... Actually pitty