Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Lithe
Lithe

Posted on

     

Orbis: The Magic of Abstraction in PHP

Have you ever wondered how to simplify complex functionalities in PHP in an elegant and reusable way? We presentOrbis, a revolutionary tool that transforms the way we manage instances and abstractions in PHP.

What is Orbis? 🤔

Orbis is a powerful class that acts as a global instance manager, allowing you to abstract complex functionalities into simple, reusable components. Imagine being able to encapsulate all routing, configuration, and state management logic in a single line of code!

The Magic Behind Orbis ✨

To understand the true power of Orbis, let's look at a real example from the Lithe framework:

functionget(string$path,callable|array...$handler):void{$caller=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,1)[0];$key=strtolower($caller['file']);$router=Orbis::instance($key);if(!$routerinstanceofRouter){thrownewException("Invalid router instance: Router not found");}$router->get($path,...$handler);}
Enter fullscreen modeExit fullscreen mode

This seemingly simple code hides an incredibly powerful functionality. Orbis allows:

  1. Each file to have its own router.
  2. Routes to be automatically managed and organized.
  3. No conflicts between different parts of the application.

A Practical Example That Will Surprise You 🚀

Let's create a smart cache system using Orbis:

classSmartCache{privatearray$storage=[];privatearray$analytics=[];publicfunctionset(string$key,mixed$value,int$ttl=3600):void{$this->storage[$key]=['value'=>$value,'expires'=>time()+$ttl,'hits'=>0];}publicfunctionget(string$key):mixed{if(!isset($this->storage[$key])){returnnull;}if(time()>$this->storage[$key]['expires']){unset($this->storage[$key]);returnnull;}$this->storage[$key]['hits']++;$this->analytics[$key]=($this->analytics[$key]??0)+1;return$this->storage[$key]['value'];}publicfunctiongetAnalytics():array{return$this->analytics;}}// Registering different instances for different contextsOrbis::register(newSmartCache(),'user.cache');Orbis::register(newSmartCache(),'product.cache');// Anywhere in your applicationfunctioncacheUser(User$user):void{$cache=Orbis::instance('user.cache');$cache->set("user.{$user->id}",$user);}functiongetUser(int$id):?User{$cache=Orbis::instance('user.cache');return$cache->get("user.{$id}");}
Enter fullscreen modeExit fullscreen mode

Why Is This Revolutionary? 🌟

  1. Automatic Isolation: Each context has its own cache instance.
  2. Zero Configuration: No need to configure anything in bootstrap or providers.
  3. Integrated Analytics: Automatic tracking of cache usage.
  4. Optimized Performance: Instances are automatically reused.

Using Orbis in Your Project

Installation via Composer:

composer require lithemod/orbis
Enter fullscreen modeExit fullscreen mode

Basic Example:

// Register an instanceOrbis::register(MyClass::class);// Use it anywhere$instance=Orbis::instance(MyClass::class);
Enter fullscreen modeExit fullscreen mode

Conclusion 🎯

Orbis is not just another dependency management library – it’s a new way of thinking about abstraction and code reuse in PHP. With it, you can:

  • Simplify complex code.
  • Improve project organization.
  • Reduce coupling between components.
  • Make testing and maintenance easier.

Try Orbis today and discover how it can transform your PHP code into something truly magical! ✨

To understand more about how Orbis works, read the postHow to Use Orbis to Simplify Your PHP Code and discover its potential in practice!

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
srsbiz profile image
Radosław Kowalewski
  • Joined

Sorry to burst your bubble, but it is not "new way" nor revolutionary. In fact, this is just like using$GLOBALS with few extra steps. Main problem with it, it's mutability - one can callunregister any time, then set something else under the same key. So anytime it is used, there should be also a check to ensure we got what we expected, hence nothing is gained by using this package.

CollapseExpand
 
lithephp profile image
Lithe
Dynamic and adaptable PHP framework.
  • Pronouns
    LithePHP
  • Joined

I understand your point, but the idea behind the package is to provide more organized control over registration and removal, which$GLOBALS doesn’t explicitly handle. While mutability is a concern, it can be managed with checks, offering more control and clarity in complex scenarios.

I see the value the package brings, but I understand your perspective as well.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Dynamic and adaptable PHP framework.
  • Pronouns
    LithePHP
  • Joined

More fromLithe

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp