/php-static-class-interface

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

Primary LanguagePHPMIT LicenseMIT

PHP Static Class Interface

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

Latest Stable Version Minimum PHP Version Build Status Coverage Status

Requirement(s)

  • PHP version from 5.6.* 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 include or require the important class.
<?php

include __DIR__ . '/src/Exception/Base.php';
include __DIR__ . '/src/Exception/ClassNamespaceResolver.php';
include __DIR__ . '/src/Exception/StaticClassAccessor.php';
include __DIR__ . '/src/Facade.php';

use LordDashMe\StaticClassInterface\Facade;

class ServiceFacade extends Facade 
{
    ...
}

Usage

  • You can start using the package without any configuration. Assuming you installed the package via Composer.

  • Create a new class that will be represent as the "Static" class of the "Service" class provided in the getStaticClassAccessor().

  • Below are the simple implementation of the package or Facade class:

<?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 converted service class that can now access like static class.
class ServiceClassFacade extends Facade
{
    public static function getStaticClassAccessor()
    {
        // The namespace of the Service Class that will convert
        // into a "static" like class.
        return '\Demo\MyClass\ServiceClass';
    }
}

// This is the main 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.