教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php实现的单例模式的例子

php实现的单例模式的例子

发布时间:2015-04-22   编辑:jiaochengji.com
在开发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.jiaochengji.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模版生成html的小例子
学习php单例模式及应用实例
学习php设计模式之单例模式
php curl模拟post请求的例子
php常用设计模式之工厂模式与单例模式介绍
php 设计模式之单例模式例子
PHP模拟POST提交的示例代码

[关闭]
~ ~