parent
ea048ab9e7
commit
f40d45d0d3
@ -1,22 +1,12 @@ |
||||
<?php |
||||
|
||||
require "libs/LazyObject.php"; |
||||
require "libs/autoload.php"; |
||||
|
||||
class MyClass { |
||||
public function __construct(public int $num) {} |
||||
public function someMethod() { |
||||
return $this->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(); |
||||
|
||||
@ -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; |
||||
} |
||||
} |
||||
@ -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