commit ae3973a5d034f6ad3e4ab063bf5f3b7062ef4ab0 Author: Robert Date: Fri Apr 4 12:36:06 2025 -0400 1st diff --git a/BcMath.php b/BcMath.php new file mode 100644 index 0000000..334e3cb --- /dev/null +++ b/BcMath.php @@ -0,0 +1,23 @@ +formatAsUSD($c); +echo "\n"; +echo $d->intlUSD($c); diff --git a/Books.php b/Books.php new file mode 100644 index 0000000..bd10338 --- /dev/null +++ b/Books.php @@ -0,0 +1,26 @@ + "Authors: David Smith\n"; + } + + public Author $mainAuthor { + get => new Author("Smith"); + set => throw new Exception("Main author cannot be changed."); + } +} + +$book = new Book(); +echo $book->credits; + +$author = $book->mainAuthor; +echo $author->name; diff --git a/Coffee.php b/Coffee.php new file mode 100644 index 0000000..6baba9f --- /dev/null +++ b/Coffee.php @@ -0,0 +1,36 @@ +value; + } +} + + +class Coffee { + public string $flavor { + get { + return $this->flavor . ' Spice'; + } + set(string $value) { + if (strlen($value) > 16) throw new InvalidArgumentException('Input is too long'); + $this->flavor = $value; + } + } + + public function serve(SizeInterface $size): void { + echo "\r\nServing a {$size->getSize()} coffee.\n"; + } +} + +$coffee = new Coffee(); +$coffee->flavor = 'Pumpkin'; +echo $coffee->flavor; + +$coffee->serve(Size::Grande); diff --git a/Dog.php b/Dog.php new file mode 100644 index 0000000..67a5674 --- /dev/null +++ b/Dog.php @@ -0,0 +1,9 @@ +bark(); +echo $dog; diff --git a/LazyCollection.php b/LazyCollection.php new file mode 100644 index 0000000..1b3222c --- /dev/null +++ b/LazyCollection.php @@ -0,0 +1,26 @@ +each(function ($line) { + echo $line; + ob_flush(); + flush(); +}); +ob_end_flush(); diff --git a/LazyGhost.php b/LazyGhost.php new file mode 100644 index 0000000..15b02b0 --- /dev/null +++ b/LazyGhost.php @@ -0,0 +1,20 @@ +__construct(1); +}; + +$reflector = new ReflectionClass(Example::class); +$object = $reflector->newLazyGhost($initializer); + +var_dump($object); +var_dump($object instanceof Example); + +// Triggers initialization, and fetches the property after that +var_dump($object->data); diff --git a/LazyObject.php b/LazyObject.php new file mode 100644 index 0000000..ce875ac --- /dev/null +++ b/LazyObject.php @@ -0,0 +1,21 @@ +num; + } +} + +class Example2 { + public function hi() { echo "Hi!"; } +} + +$object = new LazyObject()->proxy(MyClass::class, 301); +// The object is initialized only when accessed +echo $object->someMethod() . "\n"; + +$objectB = new LazyObject()->proxy(Example2::class); +$objectB->hi(); diff --git a/NullCoalescingAssignment.php b/NullCoalescingAssignment.php new file mode 100644 index 0000000..0f24057 --- /dev/null +++ b/NullCoalescingAssignment.php @@ -0,0 +1,3 @@ +moneyAsFloat($money), 2, '.', ','); + } + + public function intlUSD(Number $money): string { + if (class_exists('NumberFormatter')) { + $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); + return $formatter->formatCurrency($this->moneyAsFloat($money), 'USD'); + } + } +} diff --git a/libs/LazyCollection.php b/libs/LazyCollection.php new file mode 100644 index 0000000..24291ad --- /dev/null +++ b/libs/LazyCollection.php @@ -0,0 +1,63 @@ +source = $source; + } elseif (is_null($source)) { + $this->source = static::empty(); + } else { + $this->source = $this->getArrayableItems($source); + } + } + + public function getIterator(): Traversable + { + return $this->makeIterator($this->source); + } + + protected function makeIterator($source) + { + if ($source instanceof Closure) { + $source = $source(); + } + + if (is_array($source)) { + return new ArrayIterator($source); + } + + return $source; + } + + public static function make($source = null) + { + return new static($source instanceof Closure ? $source() : $source); + } + + public function each(callable $callback) + { + foreach ($this->getIterator() as $key => $value) { + if ($callback($value, $key) === false) { + break; + } + } + + return $this; + } + + protected function getArrayableItems($items) + { + if (is_array($items)) { + return $items; + } elseif ($items instanceof Traversable) { + return iterator_to_array($items); + } elseif (is_object($items) && method_exists($items, 'toArray')) { + return $items->toArray(); + } + + return (array) $items; + } +} diff --git a/libs/LazyObject.php b/libs/LazyObject.php new file mode 100644 index 0000000..3f50d74 --- /dev/null +++ b/libs/LazyObject.php @@ -0,0 +1,26 @@ +isInstantiable()) { + 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); + } + } +}