Luminova Framework

PHP Luminova: Application Custom Service Modules Management

Last updated: 2026-11-28 20:57:12

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

  1. Class Registration and Access: Register classes in service and access their instances anywhere in your codebase.

  2. Shared Instances: Enables multiple parts of the application to access the same instance of a class.

  3. 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::class if you didn't register your service with a custom name alias.


To get the default initialization name from YourClassName:

$instance->getName(); // Peter

To reinitialize the instance with a new constructor argument without persisting across the codebase.

$new = service('YourClassName', false, false, 'Farhana');
$new->getName(); // Farhana

To persist the initialization argument across the codebase.

$persist = service('YourClassName', true, true, 'Ujah');
$persist->getName(); // Ujah

To 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): bool

Parameters:

ParameterTypeDescription
$serviceclass-string|stringThe 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): bool

Parameters:

ParameterTypeDescription
$serviceclass-string|stringThe 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(): bool

Return:

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): object

Parameters:

ParameterTypeDescription
$servicestring|objectService class name or an existing instance.
$argumentsmixedArguments to initialize class with.
- Optional trailing booleans select serialization followed by sharing.

Return:

object - The service instance.

Throws:

Note:

Shared instances can be retrieved again in the current process. Serialization also stores a disk copy and implies sharing.