parent
ea048ab9e7
commit
f40d45d0d3
@ -1,22 +1,12 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
require "libs/LazyObject.php"; |
require "libs/autoload.php"; |
||||||
|
|
||||||
class MyClass { |
$lazyA = new libs\LazyObject(ex\MyClass::class); |
||||||
public function __construct(public int $num) {} |
$objectA = $lazyA->proxy(301); |
||||||
public function someMethod() { |
|
||||||
return $this->num; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
class Example2 { |
|
||||||
public function hi() { echo "Hi!"; } |
|
||||||
} |
|
||||||
|
|
||||||
$lazy = new LazyObject(); |
|
||||||
$object = $lazy::proxy(MyClass::class, 301); |
|
||||||
// The object is initialized only when accessed |
// 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(); |
$objectB->hi(); |
||||||
|
|||||||
@ -0,0 +1,6 @@ |
|||||||
|
<?php |
||||||
|
namespace ex; |
||||||
|
|
||||||
|
class Example2 { |
||||||
|
public function hi() { echo "Hi!"; } |
||||||
|
} |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
<?php |
||||||
|
namespace ex; |
||||||
|
|
||||||
|
class MyClass { |
||||||
|
public function __construct(public int $num) {} |
||||||
|
public function someMethod() { |
||||||
|
return $this->num; |
||||||
|
} |
||||||
|
} |
||||||
@ -1,26 +1,24 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
|
namespace libs; |
||||||
|
|
||||||
class LazyObject { |
class LazyObject { |
||||||
public static function proxy(string $className, mixed $c = null) { |
public function __construct(public string $className) {} |
||||||
|
|
||||||
|
public function proxy(mixed ...$args) { |
||||||
|
$className = $this->className; |
||||||
if (!class_exists($className, true)) { |
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()) { |
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 |
$initializer = static function () use ($className, $args) { |
||||||
if ($constructor && $constructor->getNumberOfRequiredParameters() > 0) { |
return new $className(...$args); |
||||||
$initializer = static function (mixed $i) use ($className) { |
}; |
||||||
return new $className($i); |
|
||||||
}; |
return $reflector->newLazyProxy($initializer); |
||||||
return $reflector->newLazyProxy(fn() => $initializer($c)); |
|
||||||
} else { |
|
||||||
$initializer = static function () use ($className) { |
|
||||||
return new $className(); |
|
||||||
}; |
|
||||||
return $reflector->newLazyProxy($initializer); |
|
||||||
} |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,11 @@ |
|||||||
|
<?php |
||||||
|
spl_autoload_register(function ($className) { |
||||||
|
// Project base directory |
||||||
|
$baseDir = dirname(__DIR__,1) . '/'; |
||||||
|
// Replace namespace separator with directory separator |
||||||
|
$file = $baseDir . str_replace('\\', '/', $className) . '.php'; |
||||||
|
// If the file exists, require it |
||||||
|
if (file_exists($file)) { |
||||||
|
require_once $file; |
||||||
|
} |
||||||
|
}); |
||||||
Loading…
Reference in new issue