s/fluent/decorated/

Posted by m6w6 on 30th December 2005 in Mike's sudden inspirations: PHP

I’ve read some posts about “fluent interfaces” and I have to agree that it’s a bad idea to sacrifice a reasonable API to be able to write english code.

It’s anyway possible to accomplish that to some extent on a need-by-need basis with a class of only 20 lines of code:

class ReturnThisDecorator  
{  
    private $object;  
    public $result;  
  
    function __construct($object)  
    {  
        if (is_object($object)) {  
            $this->object = $object;  
        } else {  
            throw new InvalidArgumentException(  
                "Expected an object as argument"  
            );  
        }  
    }  
  
    function __call($method, $params)  
    {  
        $this->result = call_user_func_array(  
            array($this->object, $method), $params  
        );  
        return $this;  
    }  
}  
  
$sms = new ReturnThisDecorator(new SMS);  
echo $sms->from('...')->to('...')->message('...')->send()->result;