Skip to content

Instantly share code, notes, and snippets.

@ruliarmando
Created October 29, 2014 08:55
Show Gist options
  • Save ruliarmando/884daf184100c791b763 to your computer and use it in GitHub Desktop.
Save ruliarmando/884daf184100c791b763 to your computer and use it in GitHub Desktop.
php dynamic method addition
<?php
class Foo
{
private $dynamicMethods = array();
public function __call($name, $arguments=array())
{
if(isset($this->dynamicMethods[$name]) and is_callable($this->dynamicMethods[$name])){
call_user_func_array($this->dynamicMethods[$name], $arguments);
}
}
public function __set($name, $value)
{
$this->dynamicMethods[$name] = $value;
}
}
$foo = new Foo;
$foo->bar = function(){ echo 'hello'; };
$foo->bar();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment