rants from the dark side of marketing

Deferred chain

A few months ago, I wrote a small PHP class to help me create chainable interfaces (PHP people like to call these “fluent interfaces”) without having to retrofit old code. I call this DeferredChain.

Here is the source for DeferredChain.

The idea is that you extend DeferredChain and write a bunch of methods prefixed with ‘_’. These don’t have to return $this to enable you to chain them.


class MyChain extends DeferredChain {

protected function _doSomething() {
$f = file_get_contents('something.txt');
return $f;
}

// you can have method arguments of course
protected function _doSomethingElse($x) {
return $x*2;
}
}

$m = new MyChain;
$m->doSomething()->doSomethingElse(42);

//do whatever

// execute your chain step by step
while($res = $m->doNext()) {
$results[] = $res ;
echo $res;
}

// or all steps at once
$results = $m->doAll();

If you are still reading this post, here is a more interesting example.


$d = new DiggChain($user,$pass);

$d->login()->profile()
->index()->diggStories(4)->index()
->category()->buryStories(2)
->category('general')->diggMine($story_id)
->index();

$c = new CurlBase;

while(($requests = $d->doNext())) {
$c->addArr($requests);
$c->perform();
}

Here is the incomplete DiggChain.

This is how I like to write glue for loosely coupled cURL requests. For example, I wouldn’t use chains for just a series of HTTP requests that login at a website. I have been using the Curl Objects library, which provides a specialized class for tightly coupled HTTP requests. You should probably check that out if you use php-cURL.

Well that’s it for today. Stay tuned for more coding related posts.

Posted on Tuesday, July 21st, 2009 at 9:33 am under Code. You can skip to the end and leave a response. Pinging is currently not allowed.

6 Comments

seozero Says:

There is an infinitive loop in DeferredChain->doNext. Am I right?

countzero Says:

There is no infinite loop. There were some problems with doAll(), which I just fixed though.

me again Says:

My mistake, no loop of course.

I wanted to say that $this->pos is always -1.
$k = $this->pos+1;
doNext() always executes steps[0].

countzero Says:

Seems I messed it up when I rewrote it. Thanks for pointing that out.

Victory Says:

This chaining is only concerned with function side affects i take it.

Am i missing something or won’t that digg chain blaze through the requests at super human speeds?

countzero Says:

Yeah it would. You could either sleep() in the while() loop or use CurlObject’s sleeping options.

Leave a Reply

You must be logged in to post a comment.

 

Subscribe

RSS feed

Contact


Pages


Search


Asides

Content may be king, but distribution pays the king’s mortgage.

8/12/09» 15:51» link» comments

Google acquired reCaptcha about a month ago, you might want to throttle your reCaptcha solving per IP address from now on.

14/10/09» 16:22» link» comments

Matt Cutts on how Google deals with spam.

7/10/09» 14:31» link» comments

Why you don’t want to shard.

Real World Web: Performance & Scalability.

NGINX + PHP-FPM + APC.

Gearman is interesting.

31/08/09» 4:46» link» comments
 
 
Copyright 2008, blackhat-seo.com