php实现的单例模式的例子

发布时间:2021-01-12编辑:脚本学堂
在开发php程序时经常用到数据库连接,使用单例模式,可以保证数据库的连接是只是用一个,从而也提高了程序的性能。

以下是俺写的拙作代码,供大家参考,欢迎拍砖!
 

复制代码 代码如下:
<?php
/****------------------*******
*   Copyright (C) 2007 by 耿鸿飞   *
*   ghf@localhost.localdomain   *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*  @link http://www.jb200.com
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
****------------------*******/
    define("DB_HOST","localhost");
    define("DB_USER","root");
    define("DB_PASS","");
    define("DB_NAME","test"):
  
    class DBConnect()
    {
        private static $DB;
       
        private function & getCon()
        {
            if(self::DB == nuyll)
            {
                self::DB = &mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("连接数据库失败!");
                mysql_select_db(self::DB,DB_NAME);
            }
            return self::DB;
        }
        /*************
        *        insert or update data to database;
        *  
        */
        public upDate($sql)
        {
            return mysql_query($sql,this.getCon());           
        }
        /***************
        *  query data from database and return data that type is array
        */
        public query($sql)
        {
            $rs = mysql_query($sql,this.getCon());
            $rows = array();
            $i = 0;
            while($row = mysql_fetch_array($rs))
            {
                rows[$i] = $row;
                $i++;
            }
            mysql_free_result($rs);
            return $rows;
        }
        /************
        *   query database and return data rows;
        */
        public query_num_rows($sql)
        {
            $rs = mysql_query($sql,this.getCon());
            return mysql_num_row($rs);
        }
        /**********
        *  close dbconnection;
        */
        public colse()
        {
            mysql_colse(this.getCon());
            self::DB = null;
        }
    }
?>

您可能感兴趣的文章:
php单例模式为何只能实例化一次
php设计模式之单例模式的实例代码
学习php设计模式之单例模式
学习php单例模式及应用实例
php单例模式的演示代码
有关php单例模式介绍及例子
php设计模式之单例模式学习
php单例模式的例子