-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.php
79 lines (70 loc) · 3.84 KB
/
invoke.php
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
require_once '../vendor/autoload.php';
use CommonPHP\DependencyInjection\DependencyInjector;
class ExampleClass
{
private string $property;
public function __construct()
{
$this->property = 'Initial value';
}
public function setProperty(string $newValue): void
{
$this->property = $newValue;
}
public function getProperty(): string
{
return $this->property;
}
}
$injector = new DependencyInjector();
// Instantiate ExampleClass without any constructor parameters
try {
$instance = $injector->instantiate(ExampleClass::class, []);
} catch (\CommonPHP\DependencyInjection\Exceptions\ClassNotDefinedException $e) {
// This exception is thrown when a class is not defined but attempted to be instantiated.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\ClassNotInstantiableException $e) {
// This exception is thrown when a class that is not instantiable (like an interface or abstract class) is attempted to be instantiated.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\InstantiateCircularReferenceException $e) {
// This exception is thrown when there is a circular reference detected during instantiation of a class.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\InstantiationFailedException $e) {
// This exception is thrown when instantiation of a class fails for some reason.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\ParameterDiscoveryFailedException $e) {
// This exception is thrown when there's a failure in discovering the parameters required for instantiation of a class.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\UnsupportedReflectionTypeException $e) {
// This exception is thrown when there's an unsupported reflection type encountered during parameter discovery.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
}
// Invoke setProperty method on the instantiated object
try {
$injector->invoke($instance, 'setProperty', ['newValue' => 'Updated value']);
} catch (\CommonPHP\DependencyInjection\Exceptions\InvocationFailedException $e) {
// This exception is thrown when an invocation of a method or function fails.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\MethodIsStaticException $e) {
// This exception is thrown when an attempt is made to invoke a static method as if it was non-static.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\MethodNotDefinedException $e) {
// This exception is thrown when an attempt is made to invoke a method that is not defined.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\MethodNotPublicException $e) {
// This exception is thrown when an attempt is made to invoke a method that is not public.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
}
// Use the updated object
echo $instance->getProperty(); // Outputs: Updated value