Orb

The `Orb` class is an essential part of the Lithe system, designed to efficiently and organizedly manage class instances. It allows encapsulating instances, making them easily accessible and reusable in different parts of the application. With the `Orb` class, you can avoid redundant object creation, ensuring that each instance is registered and accessed in a controlled manner.

Registering Instances

The register method is responsible for creating or registering an instance of the specified class. It accepts the class name (or an existing instance) and an optional parameter for the registration key. This is particularly useful to ensure that you have a single access point for each instance, simplifying dependency management.

// Registering a new instance of the User class
Lithe\Orbs\Orb::register(User::class);

// Registering an existing instance
$userInstance = new User();
Lithe\Orbs\Orb::register($userInstance);

With this, the User class instance can be accessed from anywhere in the application, allowing for a more cohesive and modular design.


Accessing Instances

To access a previously registered instance, you can use the instance method. It takes the key corresponding to the desired instance and can optionally unregister the instance at the same time. This is useful in scenarios where you want to ensure the instance will no longer be used after its retrieval.

// Accessing the instance of the User class
$userInstance = Lithe\Orbs\Orb::instance(User::class);

// Accessing and unregistering the instance
$anotherInstance = Lithe\Orbs\Orb::instance(User::class, true);

This way, you have full control over the lifecycle of instances in your application.


Removing Instances

If you need to remove a registered instance, use the unregister method. This is useful for freeing up memory or ensuring that an instance is no longer accessible after its use.

// Removing the instance of the User class
Lithe\Orbs\Orb::unregister(User::class);

This method prevents unnecessary instances from staying in memory, contributing to a more efficient application.


The Orb class provides a practical and efficient way to manage class instances in your Lithe application. With methods like register, instance, and unregister, you can ensure that your instances are encapsulated and accessed in a controlled manner, improving code organization and modularity.

Using the Orb class is an effective strategy for dependency management, promoting cleaner and more scalable software design.