Skip to content

Instantly share code, notes, and snippets.

@lastguest
Last active March 17, 2023 09:46
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save lastguest/4732993 to your computer and use it in GitHub Desktop.
Save lastguest/4732993 to your computer and use it in GitHub Desktop.
PHP Class Chainable.Wrapper for convenient chaining methods.
<?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;
@tdeekens
Copy link

tdeekens commented Feb 7, 2013

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment