PHP code example of m1lt0n / delegator

1. Go to this page and download the library: Download m1lt0n/delegator 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/ */

    

m1lt0n / delegator example snippets




use Delegator\DelegatorInterface;

// Assume we have a Person class
class Person implements DelegatorInterface
{
    use \Delegator\DelegatorTrait;

    // Implement the delegateMap method. This is the only construct(Address $address)
    {
        $this->address = $address;
    }
}

class Address
{
    protected $street;
    protected $number;
    protected $city;

    public function __construct($street, $number, $city)
    {
        $this->street = $street;
        $this->number = $number;
        $this->city = $city;
    }

    public function fullAddress()
    {
        return "{$this->number}, {$this->street}, {$this->city}";
    }
}

class Settings
{
    public function all()
    {
        return 'all settings';
    }

    public function something()
    {
        return 'some setting';
    }
}

// Simply by using the DelegatorTrait and implementing the delegateMap method,
// we can now delegate calls to the mapped delegates.
$address = new Address('Percy str.', 9, 'London');
$settings = new Settings();
$me = new Person($address, $settings);

echo $me->fullAddress(); // this will be delegated to $me->address->fullAddress();
echo $me->all(); // this will be delegated to $me->settings->all();

// this is equivalent to $me->address->fullAddress, but we have simplified
// our API and hide the internals, while ensuring separation of concerns and
// limited responsibility for each class.