A simple package that convert a service class into a static-like class.
- PHP version from 7.0.* up to latest.
- Use the command below to install the package via composer:
composer require lorddashme/php-static-class-interface
- You can also use this package without composer, just clone this repository and import all the important class:
<?php
include __DIR__ . '/src/Exception/FacadeException.php';
include __DIR__ . '/src/Exception/ClassNamespaceResolver.php';
include __DIR__ . '/src/Exception/StaticClassAccessor.php';
include __DIR__ . '/src/Facade.php';
use LordDashMe\StaticClassInterface\Facade;
class ServiceClassFacade extends Facade
{
protected static function getStaticClassAccessor()
{
return '\Namespace\ServiceClass';
}
}
-
You can start using the package without any configuration. Assuming the package was installed via Composer.
-
Create a new class that will represent as the Static class of the Service class.
-
Override the
getStaticClassAccessor()
and set the namespace of the target Service class. -
Below are the simple implementation of the package:
<?php
include __DIR__ . '/vendor/autoload.php';
namespace Demo\MyClass;
// Import the main class of the package.
use LordDashMe\StaticClassInterface\Facade;
// This is the original service class.
class ServiceClass
{
public function testService($context)
{
echo 'Hello World ' . $context . '!';
}
}
// This is the mimic service class that can now access like static class.
class ServiceClassFacade extends Facade
{
protected static function getStaticClassAccessor()
{
// The namespace of the Service Class that will convert
// into a "static" like class.
return '\Demo\MyClass\ServiceClass';
}
}
// This is the Service Class.
$service = new ServiceClass();
$service->testService('ServiceClass'); // echo Hello World ServiceClass!
// And we can now use the Service Class like a "static" class implementation.
ServiceClassFacade::testService('ServiceFacadeClass'); // echo Hello World ServiceFacadeClass!
This package is open-sourced software licensed under the MIT license.