initial commit
[m6w6/m6w6.github.io] / _posts / 2005-12-30-sfluentdecorated.md
1 ---
2 title: s/fluent/decorated/
3 author: m6w6
4 tags:
5 - PHP
6 ---
7
8 I've [read](http://www.achievo.org/blog/archives/25-The-danger-of-Fluent-interfaces.html)
9 [some](http://andigutmans.blogspot.com/2005/12/fluent-interfaces.html)
10 [posts](http://paul-m-jones.com/blog/?p=188) about "fluent interfaces" and
11 I have to agree that it's a bad idea to sacrifice a reasonable
12 API to be able to write english code.
13
14 It's anyway possible to accomplish that to some extent on a need-by-need basis
15 with a class of only 20 lines of code:
16
17 ```php
18 class ReturnThisDecorator
19 {
20 private $object;
21 public $result;
22
23 function __construct($object)
24 {
25 if (is_object($object)) {
26 $this->object = $object;
27 } else {
28 throw new InvalidArgumentException(
29 "Expected an object as argument"
30 );
31 }
32 }
33
34 function __call($method, $params)
35 {
36 $this->result = call_user_func_array(
37 array($this->object, $method), $params
38 );
39 return $this;
40 }
41 }
42
43 $sms = new ReturnThisDecorator(new SMS);
44 echo $sms->from('...')->to('...')->message('...')->send()->result;
45 ```