Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Created November 23, 2016 09:54
Show Gist options
  • Save SebDeclercq/7581a729c6696845eaa728325a12071c to your computer and use it in GitHub Desktop.
Save SebDeclercq/7581a729c6696845eaa728325a12071c to your computer and use it in GitHub Desktop.
<?php
class T
{
protected $methods = array();
public function addMethod($name, $closure) {
$this->methods[$name] = $closure->bindTo($this, __CLASS__);
}
function __call($method, $args) {
if (is_callable($this->methods[$method])) {
return call_user_func_array($this->methods[$method], $args);
}
}
}
$t = new T;
$t->addMethod("hello", function($name) {
echo "Hello $name !";
});
$t->hello("Seb");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment