A single-class implementation of multiple subclass inheritance in PHP

Source: Internet
Author: User
Keywords Php
Tags protected constructor
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.