PHP code example of b2r / property-method-delegator
1. Go to this page and download the library: Download b2r/property-method-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/ */
b2r / property-method-delegator example snippets
use b2r\Component\PropertyMethodDelegator\PropertyMethodDelegator;
class Foo
{
public function doFoo()
{
return __METHOD__;
}
public function execute()
{
return __METHOD__;
}
public function run()
{
return __METHOD__;
}
public function hello() {
return __METHOD__;
}
public function publicHidden()
{
return __METHOD__;
}
protected function protectedMethod()
{
return __METHOD__;
}
}
class Bar
{
public function doBar()
{
return __METHOD__;
}
public function execute()
{
return __METHOD__;
}
public function run()
{
return __METHOD__;
}
public function hello() {
return __METHOD__;
}
public function publicHidden()
{
return __METHOD__;
}
protected function protectedMethod()
{
return __METHOD__;
}
}
class FooBar
{
use PropertyMethodDelegator;
protected static $propertyMethodDelegator = [
'foo' => [
'execute' => true, // FooBar::execute invoke FooBar::$foo::execute
'foo' => 'doFoo', // FooBar::foo invoke FooBar::$foo::doFoo
'publichidden' => false, // DO NOT invoke FooBar::$foo::publicHidden
],
'bar' => [
'run' => true, // FooBar::run invoke FooBar::$bar::run
'bar' => 'doBar', // FooBar::bar invoke FooBar::$bar::doBar
'publichidden' => false, // DO NOT invoke FooBar::$bar::publicHidden
],
];
protected $foo;
protected $bar;
public function __construct()
{
$this->foo = new Foo();
$this->bar = new Bar();
}
}
$foobar = new FooBar();
echo $foobar->execute(),"\n"; #=>'Foo::execute'
echo $foobar->run(),"\n"; #=>'Bar::run'
echo $foobar->foo(),"\n"; #=>'Foo::doFoo'
echo $foobar->bar(),"\n"; #=>'Bar::doBar'
echo $foobar->doFoo(),"\n"; #=>'Foo::doFoo' Automatically resolved
echo $foobar->doBar(),"\n"; #=>'Bar::doBar' Automatically resolved
echo $foobar->hello(),"\n"; #=>'Foo::hello' Automatically resolved(foo is first)
// `protectedMethod` is protected, cannot resolve delegate method
var_dump($foobar->resolveDelegateMethod('protectedMethod')); #=> false
// `publicHidden` is hidden both foo and bar, cannot resolve delegate method
var_dump($foobar->resolveDelegateMethod('publicHidden')); #=> false
#------------------------------------------------------------
/**
* Change delegate method resolving order: bar, foo
*/
class BarFoo extends FooBar
{
protected static $propertyMethodDelegator = [
'bar' => [],
'foo' => [],
];
}
$barfoo = new BarFoo();
echo $barfoo->hello(),"\n"; #=>'Bar::hello' Automatically resolved(bar is first)
use b2r\Component\PropertyMethodDelegator\PropertyMethodDelegator;
class ArrayObjectWrapper
{
use PropertyMethodDelegator;
protected static $propertyMethodDelegator = [
'arrayObject' => [],
];
private $arrayObject;
public function __construct()
{
$this->arrayObject = new ArrayObject();
}
}
$a = new ArrayObjectWrapper();
$a->append(1);
var_dump($a->getArrayCopy()); #=>[1]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.