note 121057 added to closure.fromcallable
| From: | igorchernin at yahoo dot com | Date: | Sun, 07 May 2017 20:13:59 +0000 |
| Subject: | note 121057 added to closure.fromcallable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209057@lists.php.net to get a copy of this message | ||
It seems that the result of the "fromCallable" behaves a little bit different then an
original Lambda function.
class A {
private $name;
public function __construct($name)
{
$this->name = $name;
}
}
// test callable
function getName()
{
return $this->name;
}
$bob = new A("Bob");
$cl1 = Closure::fromCallable("getName");
$cl1 = $cl1->bindTo($bob, 'A');
//This will retrieve: Uncaught Error: Cannot access private property A::$name
$result = $cl1();
echo $result;
//But for a Lambda function
$cl2 = function() {
return $this->name;
};
$cl2 = $cl2->bindTo($bob, 'A');
$result = $cl2();
// This will print Bob
echo $result;
----
Server IP: 72.52.91.14
Probable Submitter: 95.164.52.228
----
Manual Page -- http://php.net/manual/en/closure.fromcallable.php
Edit -- https://master.php.net/note/edit/121057
Del: integrated -- https://master.php.net/note/delete/121057/integrated
Del: useless -- https://master.php.net/note/delete/121057/useless
Del: bad code -- https://master.php.net/note/delete/121057/bad+code
Del: spam -- https://master.php.net/note/delete/121057/spam
Del: non-english -- https://master.php.net/note/delete/121057/non-english
Del: in docs -- https://master.php.net/note/delete/121057/in+docs
Del: other reasons-- https://master.php.net/note/delete/121057
Reject -- https://master.php.net/note/reject/121057
Search -- https://master.php.net/manage/user-notes.php