Friday, November 21, 2008

a tasty tidbit to scratch an itch

One of my favorite perl constructs is the ability to put an if statement after a print statement:

print 'test' if($x == 2);

In php that becomes

if($x == 2) echo 'test';

Not strictly longer but the phrasing feels wrong.

Extending the chain class like so I can do this:

class Puts extends Chain{
var $val;
function __construct($val){
parent::__construct();
$this->val = $val;
}

function __if($condition){
if($condition) echo $this->val."\n";
}
}

function puts($string){
$new = new Puts($string);
return $new;
}

$x = 2;
puts('this is an assertion')->if($x == 2);

#this would be nice for debugging
puts('this is a debug message')->if(DEBUG);

1 comment:

Unknown said...

now that's just crazy talk