标签:克隆 ceo mys new 数据 echo const var_dump mysql
单例模式一般会用在数据库连接上
具体代码例子:
<?php class Mysql { private $conn; private static $instance =null; private function __construct(){ $conn = @mysql_connect(‘localhost‘,‘root‘,‘root‘); } //单例模式 public static function getInstance (){ if(! self::$instance instanceof self){ self::$instance = new self; } return self::$instance; } //防止克隆 private function __clone(){} } $mysql = Mysql::getInstance(); $mysql2 = Mysql::getInstance(); var_dump($mysql); echo ‘<hr>‘; var_dump($mysql2);
标签:克隆 ceo mys new 数据 echo const var_dump mysql
原文地址:http://www.cnblogs.com/lovebing/p/7612029.html