A few questions about Singleton mode

Source: Internet
Author: User
Recently, I started some small projects and came into contact with the single-profit mode. based on the principle of hiding information as much as possible when defining classes, I wrote the following code for classes: {code ...} excuse me, as compared with the following {code ...} what are the advantages and disadvantages, or are they just redundant? Recently, I started some small projects and came into contact with the single-profit model. based on the principle of hiding information as much as possible when defining classes, I wrote the following code for classes:

class openGate{        private $dbname='mysql:host=localhost;dbname=project';        private    $username='root';        private    $password='root';        private static $key=null;        private function __construct(){}        private function __clone(){}        public static function gateKey(){            if (self::$key==null) {                self::$key=new openGate();                return self::$key->gateWay();            }            return self::$key->gateway();        }        public function gateWay(){            return new PDO($this->dbname,$this->username,$this->password);        }    }    $person=openGate::gateKey();    var_dump($person);

Excuse me, this is better

class openGate{        private static $key=null;        private function __construct(){}        private function __clone(){}        public static function gateKey(){            if (self::$key==null) {                $dbname='mysql:host=localhost;dbname=project';                $username='root';                $password='root';                return self::$key=new PDO($dbname,$username,$password);            }            return self::$key;        }    }    $person=openGate::gateKey();    var_dump($person);

What are the advantages and disadvantages, or are they just redundant?

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.