lorddashme / php-static-class-interface
A simple package that convert a service class into a static-like class.
Installs: 2 709
Dependents: 3
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=5.6 || >=7.0 || >=7.1 || >=7.2
- phan/phan: ^1.1
Requires (Dev)
- mockery/mockery: 1.*
- phpunit/phpunit: 5.* || 6.* || 7.*
README
A simple package that convert a service class into a static-like class.
Requirement(s)
- PHP version from 7.0.* up to latest.
Install
via Composer
- Use the command below to install the package via composer:
composer require lorddashme/php-static-class-interface
via Native Way
- 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'; } }
Usage
-
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!
License
This package is open-sourced software licensed under the MIT license.