diff --git a/src/LazyObject.php b/src/LazyObject.php index be2860f..a1f448d 100644 --- a/src/LazyObject.php +++ b/src/LazyObject.php @@ -13,7 +13,16 @@ $runA = (function(int $i) use ($lazyA) { }); $lazyB = new libs\LazyObject(ex\Example2::class); +ex\Example2::macro('multiply', function (int $a, int $b) { + echo $this->version; + echo PHP_EOL; + return $a * $b; +}); + $objectB = $lazyB->proxy(); $objectB->hi(); +echo $objectB->multiply(5, 3); +echo PHP_EOL; + $runA(800); diff --git a/src/ex/Example2.php b/src/ex/Example2.php index 981836f..c7baad2 100644 --- a/src/ex/Example2.php +++ b/src/ex/Example2.php @@ -1,6 +1,10 @@ bindTo($this, static::class), $parameters); + } + + return call_user_func_array($macro, $parameters); + } + + /** + * Dynamically handle static calls to the class. + */ + public static function __callStatic(string $method, mixed $parameters): mixed + { + if (!static::hasMacro($method)) { + throw new \BadMethodCallException("Method {$method} does not exist."); + } + + $macro = static::$macros[$method]; + + if ($macro instanceof \Closure) { + return call_user_func_array(\Closure::bind($macro, null, static::class), $parameters); + } + + return call_user_func_array($macro, $parameters); + } +}