From f40d45d0d3d77c45ee52d7517fec251afa3e2756 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 4 Apr 2025 18:13:27 -0400 Subject: [PATCH] autoload --- BcMath.php | 4 ++-- LazyCollection.php | 4 ++-- LazyObject.php | 22 ++++++---------------- ex/Example2.php | 6 ++++++ ex/MyClass.php | 9 +++++++++ libs/Dollars.php | 4 +++- libs/LazyCollection.php | 17 ++++++++++------- libs/LazyObject.php | 32 +++++++++++++++----------------- libs/autoload.php | 11 +++++++++++ 9 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 ex/Example2.php create mode 100644 ex/MyClass.php create mode 100644 libs/autoload.php diff --git a/BcMath.php b/BcMath.php index 334e3cb..2054085 100644 --- a/BcMath.php +++ b/BcMath.php @@ -6,7 +6,7 @@ use BcMath\Number; -require "libs/Dollars.php"; +require "libs/autoload.php"; function init(): Number { $num1 = new Number('2200'); @@ -16,7 +16,7 @@ function init(): Number { return $money; } -$d = new Dollars(); +$d = new libs\Dollars(); $c = init(); echo $d->formatAsUSD($c); echo "\n"; diff --git a/LazyCollection.php b/LazyCollection.php index 1b3222c..1a35fbe 100644 --- a/LazyCollection.php +++ b/LazyCollection.php @@ -1,8 +1,8 @@ num; - } -} - -class Example2 { - public function hi() { echo "Hi!"; } -} - -$lazy = new LazyObject(); -$object = $lazy::proxy(MyClass::class, 301); +$lazyA = new libs\LazyObject(ex\MyClass::class); +$objectA = $lazyA->proxy(301); // The object is initialized only when accessed -echo $object->someMethod() . "\n"; +echo $objectA->someMethod() . "\n"; -$objectB = $lazy::proxy(Example2::class); +$lazyB = new libs\LazyObject(ex\Example2::class); +$objectB = $lazyB->proxy(); $objectB->hi(); diff --git a/ex/Example2.php b/ex/Example2.php new file mode 100644 index 0000000..97af204 --- /dev/null +++ b/ex/Example2.php @@ -0,0 +1,6 @@ +num; + } +} diff --git a/libs/Dollars.php b/libs/Dollars.php index e5f654b..8d2cc81 100644 --- a/libs/Dollars.php +++ b/libs/Dollars.php @@ -1,4 +1,6 @@ formatCurrency($this->moneyAsFloat($money), 'USD'); } } diff --git a/libs/LazyCollection.php b/libs/LazyCollection.php index 24291ad..cd6fd0e 100644 --- a/libs/LazyCollection.php +++ b/libs/LazyCollection.php @@ -1,11 +1,14 @@ source = $source; } elseif (is_null($source)) { $this->source = static::empty(); @@ -14,19 +17,19 @@ class LazyCollection implements IteratorAggregate } } - public function getIterator(): Traversable + public function getIterator(): \Traversable { return $this->makeIterator($this->source); } protected function makeIterator($source) { - if ($source instanceof Closure) { + if ($source instanceof \Closure) { $source = $source(); } if (is_array($source)) { - return new ArrayIterator($source); + return new \ArrayIterator($source); } return $source; @@ -34,7 +37,7 @@ class LazyCollection implements IteratorAggregate public static function make($source = null) { - return new static($source instanceof Closure ? $source() : $source); + return new static($source instanceof \Closure ? $source() : $source); } public function each(callable $callback) @@ -52,7 +55,7 @@ class LazyCollection implements IteratorAggregate { if (is_array($items)) { return $items; - } elseif ($items instanceof Traversable) { + } elseif ($items instanceof \Traversable) { return iterator_to_array($items); } elseif (is_object($items) && method_exists($items, 'toArray')) { return $items->toArray(); diff --git a/libs/LazyObject.php b/libs/LazyObject.php index b78d444..3296f98 100644 --- a/libs/LazyObject.php +++ b/libs/LazyObject.php @@ -1,26 +1,24 @@ className; if (!class_exists($className, true)) { - throw new InvalidArgumentException("Class {$className} does not exist"); + throw new \InvalidArgumentException("Class {$className} does not exist"); } - $reflector = new ReflectionClass($className); + $reflector = new \ReflectionClass($className); if (!$reflector->isInstantiable()) { - throw new InvalidArgumentException("Class {$className} cannot be instantiated"); + throw new \InvalidArgumentException("Class {$className} cannot be instantiated"); } - $constructor = $reflector->getConstructor(); - // If the class has a constructor with required parameters - if ($constructor && $constructor->getNumberOfRequiredParameters() > 0) { - $initializer = static function (mixed $i) use ($className) { - return new $className($i); - }; - return $reflector->newLazyProxy(fn() => $initializer($c)); - } else { - $initializer = static function () use ($className) { - return new $className(); - }; - return $reflector->newLazyProxy($initializer); - } + $initializer = static function () use ($className, $args) { + return new $className(...$args); + }; + + return $reflector->newLazyProxy($initializer); } } diff --git a/libs/autoload.php b/libs/autoload.php new file mode 100644 index 0000000..68202db --- /dev/null +++ b/libs/autoload.php @@ -0,0 +1,11 @@ +