PHP code example of pajarotin / compose

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

    

pajarotin / compose example snippets



namespace Pajarotin\Test\Compose;
use Pajarotin\Compose\Compose as Compose;
but we need the hexadecimal value returned by getData
// We can override getData in derived class, but we cannot inherit property: $data and method: hash()
class DonorSampleClass {
    private $data;
    private function hash()  {
        return bin2hex($this->data);
    }
    public function getData() {
        return $this->data;
    }
    public function setData($value) {
        $this->data = $value;
    }
}

// We can define the desired behaviour in a dummy "Delta" class:
class DeltaCompose {
    public function getData() {
        return $this->hash();
    }
}

// Instead of changing the visibility in DonorSampleClass and inheriting, we compose:
$compose = new Compose('DesiredFinalClass', 'Pajarotin\\Test\\Compose');
$compose->fuseClass('DonorSampleClass', 'Pajarotin\\Test\\Compose');
$compose->fuseClass('DeltaCompose', 'Pajarotin\\Test\\Compose');
$compose->deferredCompilation();

$desired = new DesiredFinalClass();
$desired->setData('Bad Wolf');
$val = $desired->getData();

echo($val);

// VALID
$closure = function($value) {
    $this->data = $value;
};
$compose->addMethod('setData', $closure, Compose::PRIVATE, Compose::INSTANCE, Compose::OVERRIDABLE, false);

// INVALID
$compose->addMethod('setData', function($value) { $this->data = $value; }, Compose::PRIVATE, Compose::STATIC, Compose::OVERRIDABLE, false);