Whats new in PHP 8.4
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
php84/libs/LazyObject.php

24 lines
730 B

<?php
namespace libs;
class LazyObject {
public function __construct(public string $className) {}
public function proxy(mixed ...$args) {
$className = $this->className;
if (!class_exists($className, true)) {
throw new \InvalidArgumentException("Class {$className} does not exist");
}
$reflector = new \ReflectionClass($className);
if (!$reflector->isInstantiable()) {
throw new \InvalidArgumentException("Class {$className} cannot be instantiated");
}
$initializer = static function () use ($className, $args) {
return new $className(...$args);
};
return $reflector->newLazyProxy($initializer);
}
}