lorddashme/php-static-class-interface

一个简单的包,可以将服务类转换为类似静态的类。

2.0.0 2018-12-20 11:03 UTC

This package is auto-updated.

Last update: 2024-09-30 01:40:34 UTC


README

一个简单的包,可以将服务类转换为类似静态的类。

Latest Stable Version Minimum PHP Version Coverage Status

要求

  • 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 许可证 许可。