note 91610 added to functions.anonymous
| From: | adotschaffhirtatsedna-softdotde at osu1 dot php dot net | Date: | Fri, 19 Jun 2009 09:55:16 +0000 |
| Subject: | note 91610 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157117@lists.php.net to get a copy of this message | ||
When using anonymous functions as properties in Classes, note that there are three name scopes: one
for constants, one for properties and one for methods. That means, you can use the same name for a
constant, for a property and for a method at a time.
Since a property can be also an anonymous function as of PHP 5.3.0, an oddity arises when they share
the same name, not meaning that there would be any conflict.
Consider the following example:
<?php
class MyClass {
const member = 1;
public $member;
public function member () {
return "method 'member'";
}
public function __construct () {
$this->member = function () {
return "anonymous function 'member'";
};
}
}
header("Content-Type: text/plain");
$myObj = new MyClass();
var_dump(MyClass::member); // int(1)
var_dump($myObj->member); // object(Closure)#2 (0) {}
var_dump($myObj->member()); // string(15) "method 'member'"
$myMember = $myObj->member;
var_dump($myMember()); // string(27) "anonymous function 'member'"
?>
That means, regular method invocations work like expected and like before. The anonymous function
instead, must be retrieved into a variable first (just like a property) and can only then be
invoked.
Best regards,
----
Server IP: 212.124.37.9
Probable Submitter: 77.179.193.185
----
Manual Page -- http://www.php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/91610
Del: integrated -- https://master.php.net/note/delete/91610/integrated
Del: useless -- https://master.php.net/note/delete/91610/useless
Del: bad code -- https://master.php.net/note/delete/91610/bad+code
Del: spam -- https://master.php.net/note/delete/91610/spam
Del: non-english -- https://master.php.net/note/delete/91610/non-english
Del: in docs -- https://master.php.net/note/delete/91610/in+docs
Del: other reasons-- https://master.php.net/note/delete/91610
Reject -- https://master.php.net/note/reject/91610
Search -- https://master.php.net/manage/user-notes.php