Created
October 29, 2014 08:55
-
-
Save ruliarmando/884daf184100c791b763 to your computer and use it in GitHub Desktop.
php dynamic method addition
This file contains hidden or 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 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