lorddashme / php-static-class-interface
一个简单的包,可以将服务类转换为类似静态的类。
2.0.0
2018-12-20 11:03 UTC
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
一个简单的包,可以将服务类转换为类似静态的类。
要求
- PHP 版本从 7.0.* 到最新版本。
安装
通过 Composer
- 使用以下命令通过 composer 安装该包
composer require lorddashme/php-static-class-interface
通过本地方式
- 您也可以不使用 composer 使用此包,只需克隆此存储库并导入所有重要类
<?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'; } }
用法
-
您可以直接开始使用此包而无需任何配置。假设包是通过 Composer 安装的。
-
创建一个新的类,该类将代表服务类的静态类。
-
重写
getStaticClassAccessor()
并设置目标服务类的命名空间。 -
以下是对包的简单实现
<?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!
许可证
此包是开源软件,根据 MIT 许可证 许可。