PHP code example of drewlabs / overloadable

1. Go to this page and download the library: Download drewlabs/overloadable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

drewlabs / overloadable example snippets


// ...
use Drewlabs\Overloadable\Overloadable;

class TestClass
{
    use Overloadable;

    public function log(...$args)
    {
        return $this->overload($args, [
            static function (ConsoleLogger $logger) {
                return $logger->log();
            },
            static function (FileLogger $logger, ?string $prefix = null) {
                return $logger->log($prefix ?? 'ERROR024');
            },
        ]);
    }
}

// ...
use Drewlabs\Overloadable\Overloadable;

class TestClass
{
    use Overloadable;

    public function log(...$args)
    {
        return $this->overload($args, [
            'log1',
            'log2'
        ]);
    }

    private function log1(ConsoleLogger $logger) 
    {
        return $logger->log();
    }

    private function log2(FileLogger $logger, ?string $prefix = null) 
    {
        return $logger->log($prefix ?? 'ERROR024');
    }
}

// index.php
// Load composer autoloaded php scripts using PSR4 implementation
w FileLogger); // Call the overload that accept an instance of FileLogger as 1st parameter
$object->log(new ConsoleLogger); // Call the overload that accept an instance of ConsoleLogger as 1st parameter

$object->log(new ConsoleLogger, 'ERROR CODE 2308'); // Throws execption as there is no overloaded function that matches