PHP code example of hakito / publisher

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

    

hakito / publisher example snippets


class Target
{
    private $foo = 'secret';
    private function bar($arg) { return $arg . 'Bar'; }

    private static $sFoo = 'staticSecret';
}

$target = new Target();
$published = new hakito\Publisher\Published($target);

// Get private property
$property = $published->foo;
// $property = 'secret';

// Set private property
$published->foo = 'outsider';
// $target->foo = 'outsider';

// call private method
$name = $published->bar('Saloon');
// $name = 'SaloonBar';


// Optional you can provide a base class in the constructor
class Derived extends Target {
    private $foo = 'derived';
}

$derived = new Derived();
$published = new hakito\Publisher\Published($derived, Target::class);
$property = $published->foo; // Gets property from Target
// $property = 'secret';

$published = new StaticPublished(Target::class);

$property = $published->sFoo;
// $property = 'staticSecret'

class Target
{
    private function methodWithReferenceArgument(&$arg) { $arg = 'hi'; }
}

$target = new Target();
$published = new hakito\Publisher\Published($target);
$val = 'initial';
$published->methodWithReferenceArgument($val);
// $val is still 'initial'

class Target
{
    private $_member = [];
    private function &methodWithReferenceReturnValue($arg) { return $this->_member; }
}

$target = new Target();
$published = new hakito\Publisher\Published($target);
$val = 'initial';
$published->methodWithReferenceReturnValue($val)['new'] = 'value';
// Target::_member is still []