-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chainable.php
56 lines (51 loc) · 1.1 KB
/
Chainable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
class Chainable
{
private $instance;
private $returns;
/**
* Constructor
*
* @return void
*/
public function __construct()
{
$args = func_get_args();
$obj = array_shift($args);
$this->instance = is_object($obj) ? $obj : new $obj($args);
$this->__resetReturns();
}
/**
* PHP magic method
*
* @param string $method method name
* @param array $args method arguments
* @return Chainable
*/
public function __call($method, $args)
{
$this->returns[$method] = call_user_func_array(array($this->instance, $method), $args);
return $this;
}
/**
* Set value of last called method to given variable
*
* @param mixed &$var variable
* @return Chainable
*/
public function __getReturn(&$var)
{
$var = array_pop($this->returns);
return $this;
}
/**
* Reset $returns propery
*
* @return Chainable
*/
public function __resetReturns()
{
$this->returns = array();
return $this;
}
}