Skip to content

Instantly share code, notes, and snippets.

@BSN4
Forked from calebporzio/chain_helper.php
Created January 30, 2019 21:42
Show Gist options
  • Save BSN4/f82654775c8283be9c32144392d628fa to your computer and use it in GitHub Desktop.
Save BSN4/f82654775c8283be9c32144392d628fa to your computer and use it in GitHub Desktop.
Handy "chain()" helper method for making non-fluent classes/objects fluent.
<?php
function chain($object)
{
return new class ($object) {
public function __construct($object)
{
$this->wrapped = $object;
}
public function __call($method, $params)
{
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', get_class($this->wrapped), $method
));
$this->wrapped->{$method}(...$params);
return $this;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment