-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaravel_facade.php
More file actions
38 lines (33 loc) · 1.02 KB
/
laravel_facade.php
File metadata and controls
38 lines (33 loc) · 1.02 KB
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
<?php
declare(strict_types=1);
class Service
{
public function doSomething()
{
return "Service is doin something.";
}
}
class FacadeForService
{
protected static $serviceInstance;
//This magic function is called when we call a static method that is not present in the class (FacadeForService)
public static function __callStatic(string $method, array $arguments): mixed
{
/*echo $method;
print_r($arguments);*/
$calledClass = get_called_class();
$serviceName = str_replace("FacadeFor", "", $calledClass);
$instance = self::getServiceInstance($serviceName);
return $instance->$method(...$arguments);
//echo $serviceName;
}
public static function getServiceInstance($serviceName)
{
if (self::$serviceInstance === null) {
self::$serviceInstance = new $serviceName();
//Real code; locate service in service container
}
return self::$serviceInstance;
}
}
echo FacadeForService::doSomething();