Last active
March 17, 2023 09:46
PHP Class Chainable.Wrapper for convenient chaining methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Chainable | |
* Wrapper for convenient chaining methods | |
* | |
* @author Stefano Azzolini <lastguest@gmail.com> | |
*/ | |
class Chainable { | |
private $instance = null; | |
private $_returns = []; | |
/** | |
* Constructor accepts object instances or class name string with optionals instance contructor parameters | |
*/ | |
public function __construct(){ | |
$pars = func_get_args(); | |
$this->instance = is_object($obj=array_shift($pars))?$obj:new $obj($pars); | |
} | |
/** | |
* Retrieve the returned value from last chained method | |
* @param mixed $var The var container for the returned value | |
* @return Chainable Pass the Chainable instance for method chaining. | |
*/ | |
public function _get_return(&$var){ | |
$var = count($this->_returns)?array_pop($this->_returns):null; | |
return $this; | |
} | |
/** | |
* Clear the returned value cache | |
* @return Chainable Pass the Chainable instance for method chaining. | |
*/ | |
public function _reset(){ | |
$this->_returns = []; | |
return $this; | |
} | |
/** | |
* Method calling handler, this magic method pass the call to the embedded instance. | |
* @return Chainable Pass the Chainable instance for method chaining. | |
*/ | |
public function __call($n,$p){ | |
($r=call_user_func_array([$this->instance,$n],$p))?$this->_returns[]=$r:null; | |
return $this; | |
} | |
} | |
/** | |
* Example | |
*/ | |
class Test { | |
function foo(){ echo "FOO"; } | |
function baz(){ return "BAZ"; } | |
function bar(){ echo "BAR"; } | |
} | |
// Create a new chainable instance of Test | |
$t = new Chainable('Test'); | |
$t | |
->foo() | |
->baz() ->_get_return($the_value) // retrieve the returned value | |
->bar(); | |
echo "\n\n",'The value was : ',$the_value; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good stuff. Reminds me of _underscore's chaining. Took some time to get my head around the return variable being passed as an reference to not break the chainability.
Looking forward to using this...