note 119153 added to language.oop5.anonymous
| From: | Theriault | Date: | Mon, 11 Apr 2016 19:23:58 +0000 |
| Subject: | note 119153 added to language.oop5.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205202@lists.php.net to get a copy of this message | ||
Despite being anonymous, anonymous classes are named. get_declared_classes() will return anonymous
classes (PHP 7.0.5).
Because they are named, an anonymous class can also be aliased (not that its useful):
<?php
$a = new class { public $test = 123; };
class_alias(get_class($a), "Test");
$b = new Test;
echo $b->test; // outputs 123
?>
Unlike anonymous functions, returning an anonymous class from a function will be a new instance of
the SAME anonymous class each time.
<?php
function anonymous_function () {
return function () {
static $shared = 0;
echo ++$shared;
};
};
function anonymous_class() {
return new class {
static $shared = 0;
public function __construct() {
echo ++self::$shared;
}
};
}
anonymous_function(); // outputs 1
anonymous_function(); // outputs 1
anonymous_class(); // outputs 1
anonymous_class(); // outputs 2
get_class(anonymous_class()) === get_class(anonymous_class()); // TRUE
?>
----
Server IP: 72.52.91.14
Probable Submitter: 2601:281:c401:1cc1:10dd:a922:6a95:d2cb
----
Manual Page -- http://php.net/manual/en/language.oop5.anonymous.php
Edit -- https://master.php.net/note/edit/119153
Del: integrated -- https://master.php.net/note/delete/119153/integrated
Del: useless -- https://master.php.net/note/delete/119153/useless
Del: bad code -- https://master.php.net/note/delete/119153/bad+code
Del: spam -- https://master.php.net/note/delete/119153/spam
Del: non-english -- https://master.php.net/note/delete/119153/non-english
Del: in docs -- https://master.php.net/note/delete/119153/in+docs
Del: other reasons-- https://master.php.net/note/delete/119153
Reject -- https://master.php.net/note/reject/119153
Search -- https://master.php.net/manage/user-notes.php