Fluent PHP and pooQuery revisited

Tags:

pooQuery, my revolutionary fluent PHP library, has been an interesting project so far.

Don’t know what I’m talking about? Read the original post about pooQuery and the look at pooQuery internals.

It has caused some funny reactions from people, and perhaps it’s time to officially announce that pooQuery is a joke. A fully functional joke, which I’ve just updated – I uploaded some new code to the pooQuery Svn repository which enable you to do looping – Yes, looping with pQ syntax.

But what do we learn from pooQuery itself and in regards to fluent interfaces?

pooQuery

It all began as a thought: “Would it be possible to do a fluent jQuery’ish library for PHP? And if, how?”

As you can see, yes, it indeed is possible. The code has evolved a bit from the first implementation, especially because conditionals and looping required more complicated logic. One guy said it isn’t possible “because PHP doesn’t support things like that”, but what do you know, I was right after all…

One of the interesting things we can learn from pooQuery’s code is that you can use PHP’s reserved words as method names in classes with a bit of hackery. “If” for example, is used in conditionals in pQ. This is all thanks to the magic method __call – you can’t define a function called “if” in your class as it’ll cause an error, but if you use __call to parse it, it’ll work. At the moment I can’t think of any other language which would let you do something like this.

We can also see that it would be possible to wrap PHP functionality in objects. Even though pQ doesn’t really do anything useful, you could apply the methodology used in it to create something useful, such as OOP wrappers for strings or numbers.

Fluency fails

We could also come to the quick conclusion that fluent interfaces suck. This is not true, though.

When used correctly, fluent interfaces can allow you to perform the same tasks in less code, and often thanks to this, you also end up with code that’s easier to read.

Still, it’s easy to go overboard, and when you go, you end up with pQ. Now, I don’t know the names of the guilty libraries, but I’ve seen implementations of Regular Expressions and SQL as a fluent interface, which is just plain wrong. A commenter says this about pooQuery:

“[ snip ] Let’s face it, to develop you need to learn some sort of language syntax. I don’t think this is any easier, and is actually harder than the language itself.”

This is what is wrong with the fluent SQL and Regex libraries. You would be better off learning SQL or Regex, rather than the fluent version. If you know SQL, you can re-use the knowledge in most future programming languages you might end up working with. If you just knew the language-specific fluent SQL library, you would need to learn SQL in the case you couldn’t use the fluent library anymore.

In closing

It looks like there will be more fluent languages coming. It is kind of a trend of “Web 2.0” I guess. I’m not saying it’s necessarily a bad thing: It can be a good thing too, like jQuery shows us. It can make things nice and simple for the users of the libraries. Just keep in mind that over-engineering isn’t a good thing.

I still intend to continue developing pooQuery. I also have intended to continue developing all my other projects, like my CMS, my web browser, my games and my other apps… but look what has happened to them… all rotting away… So who knows what’ll happen to pooQuery…