class Parents{ protected $client; protected function __construct() { //一系列操作.... $this->client = new $this->clientName(); } public static function getInstance() { if (static::$ins === null) { static::$ins = new static(); } return static::$ins; }}class Son1{ protected static $ins = null; protected $clientName = 'Test1';}class Son2{ protected static $ins = null; protected $clientName = 'Test2';}
There are 1 parent classes, many subclasses, and now I want each subclass to implement a singleton, so I have the above notation. Because each subclass except the client's $clientName
not the same, the other operation is the same, in order not to be implemented in each subclass, I use the above inheritance, because each $clientName
instance will open the socket
connection, so I want to use the singleton mode, Is there any problem or inconvenience in this way above? How to fix it
Reply content:
class Parents{ protected $client; protected function __construct() { //一系列操作.... $this->client = new $this->clientName(); } public static function getInstance() { if (static::$ins === null) { static::$ins = new static(); } return static::$ins; }}class Son1{ protected static $ins = null; protected $clientName = 'Test1';}class Son2{ protected static $ins = null; protected $clientName = 'Test2';}
There are 1 parent classes, many subclasses, and now I want each subclass to implement a singleton, so I have the above notation. Because each subclass except the client's $clientName
not the same, the other operation is the same, in order not to be implemented in each subclass, I use the above inheritance, because each $clientName
instance will open the socket
connection, so I want to use the singleton mode, Is there any problem or inconvenience in this way above? How to fix it
Pro, do I want to ask your code to test it? The protected constructor is paired with a public getinstance ... You can't get an object instance at all?
You manage the connection pool with a single instance, and it's much more troublesome to get so many subclasses.