note 115999 deleted from closure.bind by salathe
| From: | salathe@php.net | Date: | Fri, 07 Nov 2014 10:18:36 +0000 |
| Subject: | note 115999 deleted from closure.bind by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200805@lists.php.net to get a copy of this message | ||
Note Submitter: jaaf64 at zoraldia dot com
----
To make the previous example easier to catch and test for a learner of the language:
<?php
trait MetaTrait
{
private $methods = array();
public function addMethod($methodName, $methodCallable)
{
if (!is_callable($methodCallable)) {
throw new InvalidArgumentException('Second param must be callable');
}
$this->methods[$methodName] = Closure::bind($methodCallable, $this, get_class());
}
public function __call($methodName, array $args)
{
if (isset($this->methods[$methodName])) {
return ('Method "'.$methodName.'" exists and returns:
'.call_user_func_array($this->methods[$methodName], $args));
}
return('Method "' .$methodName.'": There is no method with this
name to call');
}
}
?>
<?php
require 'MetaTrait.php';
class HackThursday {
use MetaTrait;
private $dayOfWeek = 'Thursday';
}
$test = new HackThursday();
$test->addMethod('when', function () {
return $this->dayOfWeek;
});
echo $test->when();
echo '<br/>'.$test->phony();
?>