php实现的单例模式

我们在开发php的时候经常的要用到数据库的连接,但是数据库的连接一般是很好资源的,为此,可以设计单例模式,以此来保证数据库的连接是只是用一个,无 论你在那里要于数据库交互,这样就保证了应用程序的整体的性能比一般的模式优一点,在面向过程的习惯中很难的使用单例模式,当然这是对于习惯于面向对象的 变成人员来说的,如果使用过程变成的模式是完全可以的,之不过我们还是要设置全局的变量来保存数据库的连接,但是这样的华就会在我们的成语中有所限制,假 设你的 程序可能包含很多的文件,总的行数超过3000行或者是更多的华,你就要面对变量的问题而焦虑了,为此改用oo的方式还是很不错的选择,现在我就把我的实 现方式于大家分享,据提到你的应用可能还不可以,你可以作必要的修改一服和你的程序。现便是代码的实现。
欢迎你来批评。
<?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.,                                       *
*   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;
        }
    }
?>
:D换成:D
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值