PHP Luminova: Application Custom Service Modules Management
Resolve registered application services, reuse instances, and manage their cached copies.
The Service class is a utility designed to simplify the management of class instances across your application. Serving as a centralized hub, it allows developers to manage, cache, or return shared instances of a class throughout the codebase. With a centralized location for registering classes, Services optimizes application performance by loading and initializing classes only when called. See service registration for an end-to-end example.
Key Features
Class Registration and Access: Register classes in service and access their instances anywhere in your codebase.
Shared Instances: Enables multiple parts of the application to access the same instance of a class.
Serialization and Caching: Supports serialization of class instances, allowing them to be stored as cache. These cached instances can be reinitialized with new constructor arguments or used with default arguments as they were stored.
Usages
Suppose you have a class named YourClassName that you want to discover using the service class loader. In this example, let's assume that your class is located at app/Utils/YourClassName.php, with the namespace App\Utils.
// /app/Utils/YourClassName.php
namespace App\Utils;
class YourClassName
{
private string $name = '';
public function __construct(string $name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
}After registering the class, you can use the Service::YourClassName() method with constructor arguments followed by the serialization and sharing flags:
use Luminova\Foundation\Module\Service;
$instance = Service::YourClassName('Peter', false, true);Global Helper Function
Additionally, there is the Luminova\Funcs\service() helper available, which provides a convenient way to access service instances.
use function Luminova\Funcs\service;
$instance = service('YourClassName', true, false, 'Peter');Using full qualified class name FooClass::class.
use App\Utils\YourClassName;
$instance = service(YourClassName::class, true, false, 'Peter');Note:
You can only use class name
YourClassName::classif you didn't register your service with a custom name alias.
To get the default initialization name from YourClassName:
$instance->getName(); // PeterTo reinitialize the instance with a new constructor argument without persisting across the codebase.
$new = service('YourClassName', false, false, 'Farhana');
$new->getName(); // FarhanaTo persist the initialization argument across the codebase.
$persist = service('YourClassName', true, true, 'Ujah');
$persist->getName(); // UjahTo get instance of service
$service = service();Clear all services.
$cleared = service()->clear();Class Definition
- Full namespace:
Luminova\Foundation\Module\Service - This class is final.
Methods
has
Check if service class is available either in cache, shared or ready to initialized.
public static function has(string $service): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$service | class-string|string | The service class name or class name alias. |
Return:bool - Return true if service class exists, false otherwise.
delete
To delete a service from shared instance and it cached instances.
public static function delete(string $service): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$service | class-string|string | The service class name or alias. |
Return:bool - Return true if the serialized file was deleted. Clearing the in-memory instance or registration alone can still return false.
To fully delete a service, you will need to unregister it from the bootstrap file
/app/Config/Services.php.
clear
To clear all instance of service and it corresponding cached instance.
public static function clear(): boolReturn:
bool - Returns whether the cache directory was deleted. In-memory registrations and instances are cleared even when the directory is absent.
add
To add a service class object or class name to services.
public static function add(string|object $service, mixed ...$arguments): objectParameters:
| Parameter | Type | Description |
|---|---|---|
$service | string|object | Service class name or an existing instance. |
$arguments | mixed | Arguments to initialize class with. - Optional trailing booleans select serialization followed by sharing. |
Return:
object - The service instance.
Throws:
- Luminova\Exceptions\RuntimeException - If service already exist or unable to initiate class.
Note:
Shared instances can be retrieved again in the current process. Serialization also stores a disk copy and implies sharing.