Link Search Menu Expand Document

Examples

Table of contents

  1. Example with inheritance
  2. Example with composition
  3. Example of an instance extension method
    1. Instance extension method registration
    2. Instance extension method usage
  4. Example of a static extension method
    1. Static extension method registration
    2. Static extension method usage

The following examples are equivalent, each having its caveats.

Example with inheritance

<?php

declare(strict_type=1);

namespace NorseBlue\ExtensibleObjects\Examples;

use NorseBlue\ExtensibleObjects\ExtensibleObject;

class Path extends ExtensibleObject
{
    private $path;
    
    public function __construct(string $path)
    {
        $this->path = $path;
    }

    public function __toString()
    {
        return $this->path;
    }
}

Example with composition

<?php

declare(strict_type=1);

namespace NorseBlue\ExtensibleObjects\Examples;

use NorseBlue\ExtensibleObjects\Contracts\Extensible;
use NorseBlue\ExtensibleObjects\Traits\HandlesExtensionMethods;

class Path implements Extensible
{
    use HandlesExtensionMethods;

    private $path;
    
    public function __construct(string $path)
    {
        $this->path = $path;
    }

    public function __toString()
    {
        return $this->path;
    }
}

Example of an instance extension method

<?php

declare(strict_type=1);

namespace NorseBlue\ExtensibleObjects\Examples;

use NorseBlue\ExtensibleObjects\Contracts\ExtensionMethod;

class AppendExtension extends Path implements ExtensionMethod
{
    public function __invoke(): callable
    {
        return function(string $append): string {
            return $this->path . $append;
        };
    }
}

Instance extension method registration

<?php

namespace NorseBlue\ExtensibleObjects\Examples;

Path::registerExtensionMethod('append', AppendExtension::class);

Instance extension method usage

<?php

namespace NorseBlue\ExtensibleObjects\Examples;

$path = new Path('/home');

echo $path->append('/norse-blue');

// Outputs: /home/norse-blue

Example of a static extension method

<?php

declare(strict_type=1);

namespace NorseBlue\ExtensibleObjects\Examples;

class BuildExtension extends Path implements ExtensionMethod
{
    public function __invoke(): callable
    {
        return static function(array $segments): Path {
            return new Path(DIRECTORY_SEPARATOR, $segments);
        };
    }
}

Static extension method registration

<?php

namespace NorseBlue\ExtensibleObjects\Examples;

Path::registerExtensionMethod('build', BuildExtension::class);

Static extension method usage

<?php

namespace NorseBlue\ExtensibleObjects\Examples;

$path = Path::build(['', 'home', 'norse-blue']);

echo $path;

// Outputs: /home/norse-blue