项目作者: LordDashMe

项目描述 :
A simple package that convert a service class into a static-like class.
高级语言: PHP
项目地址: git://github.com/LordDashMe/php-static-class-interface.git
创建时间: 2018-08-08T08:22:07Z
项目社区:https://github.com/LordDashMe/php-static-class-interface

开源协议:MIT License

下载


PHP Static Class Interface

A simple package that convert a service class into a static-like class.

Latest Stable Version Minimum PHP Version Coverage Status

Requirement(s)

  • PHP version from 7.0.* up to latest.

Install

via Composer

  • Use the command below to install the package via composer:
  1. 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:
  1. <?php
  2. include __DIR__ . '/src/Exception/FacadeException.php';
  3. include __DIR__ . '/src/Exception/ClassNamespaceResolver.php';
  4. include __DIR__ . '/src/Exception/StaticClassAccessor.php';
  5. include __DIR__ . '/src/Facade.php';
  6. use LordDashMe\StaticClassInterface\Facade;
  7. class ServiceClassFacade extends Facade
  8. {
  9. protected static function getStaticClassAccessor()
  10. {
  11. return '\Namespace\ServiceClass';
  12. }
  13. }

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:

  1. <?php
  2. include __DIR__ . '/vendor/autoload.php';
  3. namespace Demo\MyClass;
  4. // Import the main class of the package.
  5. use LordDashMe\StaticClassInterface\Facade;
  6. // This is the original service class.
  7. class ServiceClass
  8. {
  9. public function testService($context)
  10. {
  11. echo 'Hello World ' . $context . '!';
  12. }
  13. }
  14. // This is the mimic service class that can now access like static class.
  15. class ServiceClassFacade extends Facade
  16. {
  17. protected static function getStaticClassAccessor()
  18. {
  19. // The namespace of the Service Class that will convert
  20. // into a "static" like class.
  21. return '\Demo\MyClass\ServiceClass';
  22. }
  23. }
  24. // This is the Service Class.
  25. $service = new ServiceClass();
  26. $service->testService('ServiceClass'); // echo Hello World ServiceClass!
  27. // And we can now use the Service Class like a "static" class implementation.
  28. ServiceClassFacade::testService('ServiceFacadeClass'); // echo Hello World ServiceFacadeClass!

License

This package is open-sourced software licensed under the MIT license.