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.
24 lines
750 B
24 lines
750 B
<?php
|
|
|
|
namespace CodeHydrater;
|
|
|
|
class lazy_object {
|
|
public function __construct(public string $className) {}
|
|
|
|
public function proxy(mixed ...$args): Object {
|
|
$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);
|
|
}
|
|
}
|
|
|