-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazy_inizialization.php
More file actions
34 lines (26 loc) · 925 Bytes
/
lazy_inizialization.php
File metadata and controls
34 lines (26 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
declare(strict_types=1);
class ImageProcessor
{
public function __construct(
public string $processorName = 'DefaultProcessor',
public int $quality = 85
) {
echo "Constructor called (expensive init)" . "<br>";
}
public function process(string $image): string
{
echo "Processing $image with {$this->quality}" . "<br>";
return "processed_$image" . "<br>";
}
public function metadata(string $path): array|string
{
return "im not touching class properties" . "<br>";
}
}
$reflector = new ReflectionClass(ImageProcessor::class);
$lazyService = $reflector->newLazyGhost(
fn(ImageProcessor $instance) => $instance->__construct()
);
echo $lazyService->metadata("photo.jpg"); //Doesn't trigger initialization
echo $lazyService->process("photo.jpg"); //The process method touch a property of the class, so it will init the construct