php引用数据库实例,PHP单例模式实例,连接数据库对类的引用
//單例模式連接數(shù)據(jù)庫
class pzhang{
static private $instance;
private static $config;
private $dbase = array(
'host' => 'localhost',
'username' =>'root',
'password'=>'root',
'dbname' =>'jmyp'
);
private function __construct(){
}
static public function getInstance(){
if(!self::$instance instanceof self)
self::$instance = new self();
return self::$instance;
}
public function conn(){
$mysql_db = $this->dbase['dbname'];
self::$config = new mysqli($this->dbase['host'],$this->dbase['username'],$this->dbase['password']);
self::$config->query('set name utf8');
self::$config -> select_db($mysql_db);
$sql = "select * from admin";
$row = self::$config->query($sql);
$data = [];
while($tmp = $row->fetch_assoc()){
$data[] = $tmp;
}
echo "
";print_r($data);
echo "
";return self::$config;
}
}
$obj = pzhang::getInstance();
$obj->conn();
//單例模式對類的引用
class zhangp{
public function system(){
echo "learning more";
}
}
class singleCase{
static private $instance;
private $avg;
private function __construct($config){
self::$instance = new $config;
}
static public function getInstance($avg){
if(!self::$instance instanceof self)
new self($avg);
return self::$instance;
}
}
$obj = singleCase::getInstance('zhangp');
$obj->system();
?>
總結(jié)
以上是生活随笔為你收集整理的php引用数据库实例,PHP单例模式实例,连接数据库对类的引用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab ga rbf,GA PSO
- 下一篇: c++驱动键鼠源码,C++ 驱动打印机源