PHP Method weaving! Mix in the same method with several different signatures.
Basically the DelegateClass allows you to proxy method calls to the contained objects within. It verifies method signature so will match up the first method that will work with the supplied arguments.
<?php
class Model {
public function getName(){
return "name";
}
}
class Test extends DelegateClass {}
$test = new Test(new Model());
echo $test->getName();<?php
class Model1 {
public function say(){
return "hello";
}
}
class Model2 {
public function say($what){
return "hello " . $what;
}
}
class Test extends DelegateClass {}
$test = new Test(new Model1(), new Model2());
echo $test->say();
echo $test->say("world");<?php
class Model {
public function getName(){
return "name";
}
}
class Test extends DelegateClass {}
$model = new Model();
$test = new Test();
// Runtime binding
$test->bind($model);
echo $test->getName();
// Runtime unbinding!
$test->unbind($model);
echo $test->getName(); // <- results in BadMethodCallException!<?php
class Model {
public function say($first, $second, $third = null){
return "hello $first, $second, $third";
}
}
class Test extends DelegateClass {}
// Execution
$test = new Test(new Model());
echo $test->send("say", 1, 2);
echo $test->send("say", 1, 2, 3);<?php
class Model {
public function say($first, $second, $third = null){
return "hello $first, $second, $third";
}
}
class Test extends DelegateClass {}
// Execution
$test = new Test(new Model());
$methodName = "say";
if($test->respond_to($methodName)){ // => true
echo $test->send($methodName, 1, 2, 3);
};