note 126144 added to function.get-class
| From: | davidsch at main dot php dot net | Date: | Mon, 07 Jun 2021 02:50:54 +0000 |
| Subject: | note 126144 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216481@lists.php.net to get a copy of this message | ||
There are discussions below regarding how to create a singleton that allows subclassing. It seems
with get_called_class there is now a cleaner solution than what is discussed below, that does not
require overriding a method per subclass.
e.g.
<?php
abstract class MySuperclass {
static private $instances = array();
static public function getInstance(): ACFBlock {
$className = get_called_class();
if (!isset(self::$instances[$className])) {
self::$instances[$className] = new static();
}
return self::$instances[$className];
}
private function __construct() {
// Singleton
}
}
class MySubclass extends MySuperclass {
}
MySubclass::getInstance();
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 75.168.53.108)
----
Manual Page -- https://php.net/manual/en/function.get-class.php
Edit -- https://main.php.net/note/edit/126144
Del: integrated -- https://main.php.net/note/delete/126144/integrated
Del: useless -- https://main.php.net/note/delete/126144/useless
Del: bad code -- https://main.php.net/note/delete/126144/bad+code
Del: spam -- https://main.php.net/note/delete/126144/spam
Del: non-english -- https://main.php.net/note/delete/126144/non-english
Del: in docs -- https://main.php.net/note/delete/126144/in+docs
Del: other reasons-- https://main.php.net/note/delete/126144
Reject -- https://main.php.net/note/reject/126144
Search -- https://main.php.net/manage/user-notes.php