method chaining going too far?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

method chaining going too far?

Post by koen.h »

Everyone tries to keep things simple. Is your first reaction that this could be an API that is still simple enough?

Code: Select all

 
$a->createB()->where()->get('name')->eq()->str('value');
$a->createB()->where()->creationDate()->between->unix($x, $y);
$a->createB()->where()->getName()->lesserThan->int($value);
 
Working with things makes it easier. So I'm looking for first reactions. Eg wouldn't it scare people away?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: method chaining going too far?

Post by alex.barylski »

What I find most intereating about chaining, is that if it's done properly the result is a quasi-domain specific language (DSL). That being said, if you don't name the methods correctly it can be a real mess.

DRY KISS is what is important to follow when implementing chaining.

Your code is fairly readable. I implemented something similar for validation and SQL -- it's quite common.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: method chaining going too far?

Post by josh »

I would build up a criterion object with a fluent interface, assign the instance to a var and pass it to the where method, having the where method accept a fully formed criterion which is created separately, that way all your commands refer to the same subsystem ( within a given method chain ). I dont think its unnecessarily unreadable though. Take a look at zend, when you use where the condition gets passed as a paramater to the where function, it is not chained on... that is the way to go I think

like $select->from( 'blah' )->where( 'foo', 1 )

For both from and where their "subject" in the sentence is the select itself. In your example depending on the last command we could be talking about the select or the condition of the select, which is implied and not explicit in the API.

or ->where( 'some complex sql' )

or ->where( $someObjrepresentingCriteria )

These kinds of decisions are really easy to make when doing TDD.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: method chaining going too far?

Post by Ollie Saunders »

I think what you're trying to implement is called ROE (relational object expressions), if you're planning on using this to interact with a relational database. It was done in Smalltalk some time ago. I've tried to find information on it in the past with little success.

I'm interested in it as an alternative to ORMs but I suspect a full ROE layer would be a pretty big undertaking.
Eg wouldn't it scare people away?
That's a terrible thing to ask. Write what you think will work best; if it does work well, tell people that it works well; if they think it's too different for them it'll be their loss. The question is: Is this an effective API?
Post Reply