note 115999 added to closure.bind
| From: | jaaf64 at zoraldia dot com | Date: | Mon, 27 Oct 2014 05:59:40 +0000 |
| Subject: | note 115999 added to closure.bind | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200736@lists.php.net to get a copy of this message | ||
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();
?>
----
Server IP: 72.52.91.14
Probable Submitter: 109.220.245.228
----
Manual Page -- http://php.net/manual/en/closure.bind.php
Edit -- https://master.php.net/note/edit/115999
Del: integrated -- https://master.php.net/note/delete/115999/integrated
Del: useless -- https://master.php.net/note/delete/115999/useless
Del: bad code -- https://master.php.net/note/delete/115999/bad+code
Del: spam -- https://master.php.net/note/delete/115999/spam
Del: non-english -- https://master.php.net/note/delete/115999/non-english
Del: in docs -- https://master.php.net/note/delete/115999/in+docs
Del: other reasons-- https://master.php.net/note/delete/115999
Reject -- https://master.php.net/note/reject/115999
Search -- https://master.php.net/manage/user-notes.php