lorddashme/php-static-class-interface

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

2.0.0 2018-12-20 11:03 UTC

This package is auto-updated.

Last update: 2024-10-30 01:53:44 UTC


README

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:
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.