note 124848 added to function.get-called-class
| From: | amcolin at 126 dot com | Date: | Tue, 24 Mar 2020 07:08:18 +0000 |
| Subject: | note 124848 added to function.get-called-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214369@lists.php.net to get a copy of this message | ||
namespace root;
class Factor {
protected static $instance = null;
private function __construct() {
}
public static function getInstance() {
if (!self::$instance) {
$name = get_called_class();
self::$instance = new $name();
}
return self::$instance;
}
}
namespace admin\test;
use root\Factor;
class Single extends Factor {
public function abc() {
return 'abc';
}
}
namespace index;
use admin\test\Single;
class Index {
public function get() {
return Single::getInstance();
}
}
$index = new Index();
var_dump($index->get());
The result is:
object(admin\test\Single)#2 (0) {
}
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 180.169.253.5)
----
Manual Page -- http://php.net/manual/en/function.get-called-class.php
Edit -- https://master.php.net/note/edit/124848
Del: integrated -- https://master.php.net/note/delete/124848/integrated
Del: useless -- https://master.php.net/note/delete/124848/useless
Del: bad code -- https://master.php.net/note/delete/124848/bad+code
Del: spam -- https://master.php.net/note/delete/124848/spam
Del: non-english -- https://master.php.net/note/delete/124848/non-english
Del: in docs -- https://master.php.net/note/delete/124848/in+docs
Del: other reasons-- https://master.php.net/note/delete/124848
Reject -- https://master.php.net/note/reject/124848
Search -- https://master.php.net/manage/user-notes.php