Adding Method to an Object on the Fly in PHP

If you need to add functions to an Object on the fly in PHP, here is a possible solution:

class MyClass
{
    public function __call($m, $a)
    {
        if (isset($this->$m)) {
            $func = $this->$m;
            $func();
        }
    }
}
 
$test = new MyClass();
$test ->runtimeAddedFunction = function () { echo "It Works !"; };
$test ->runtimeAddedFunction();