diff --git a/src/MagicMethods.php b/src/MagicMethods.php index 1fa86c4..89c6108 100644 --- a/src/MagicMethods.php +++ b/src/MagicMethods.php @@ -25,9 +25,9 @@ public function __call($method, $arguments) $inner_method = "get" . ucfirst($method); if (method_exists($this, $inner_method)) { $value = call_user_func([$this, $inner_method]); - /* If value is callable run it. */ + /* If value is anonymous function run it. */ if ($value instanceof \Closure) { - return call_user_func($value, $arguments[0]); + return call_user_func_array($value, $arguments); } } } diff --git a/test/MagicMethodsTest.php b/test/MagicMethodsTest.php index 499a272..f360bd4 100644 --- a/test/MagicMethodsTest.php +++ b/test/MagicMethodsTest.php @@ -76,4 +76,18 @@ public function testShouldGetAndSetDynamicFunction() }; $this->assertEquals($unicorn->dynamic("beer"), "No beer!"); } + + public function testShouldGetAndSetDynamicFunctionWithManyParameters() + { + $unicorn = new Unicorn(); + $unicorn->dynamic(function ($foo, $bar) { + return "Got {$foo} and {$bar}!"; + }); + $this->assertEquals($unicorn->dynamic("milk", "honey"), "Got milk and honey!"); + + $unicorn->dynamic = function ($foo, $bar) { + return "No {$foo} or {$bar}!"; + }; + $this->assertEquals($unicorn->dynamic("beer", "vodka"), "No beer or vodka!"); + } }