Home  >  Article  >  Backend Development  >  Example of singleton pattern implemented in php

Example of singleton pattern implemented in php

WBOY
WBOYOriginal
2016-07-25 09:02:491038browse
  1. /****------------------*******
  2. * Copyright (C) 2007 by 耿鸿飞 *
  3. * ghf@localhost.localdomain *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * @link http://bbs.it-home.org
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ****------------------*******/
  21. define("DB_HOST","localhost");
  22. define("DB_USER","root");
  23. define("DB_PASS","");
  24. define("DB_NAME","test"):
  25. class DBConnect()
  26. {
  27. private static $DB;
  28. private function & getCon()
  29. {
  30. if(self::DB == nuyll)
  31. {
  32. self::DB = &mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("连接数据库失败!");
  33. mysql_select_db(self::DB,DB_NAME);
  34. }
  35. return self::DB;
  36. }
  37. /*************
  38. * insert or update data to database;
  39. *
  40. */
  41. public upDate($sql)
  42. {
  43. return mysql_query($sql,this.getCon());
  44. }
  45. /***************
  46. * query data from database and return data that type is array
  47. */
  48. public query($sql)
  49. {
  50. $rs = mysql_query($sql,this.getCon());
  51. $rows = array();
  52. $i = 0;
  53. while($row = mysql_fetch_array($rs))
  54. {
  55. rows[$i] = $row;
  56. $i++;
  57. }
  58. mysql_free_result($rs);
  59. return $rows;
  60. }
  61. /************
  62. * query database and return data rows;
  63. */
  64. public query_num_rows($sql)
  65. {
  66. $rs = mysql_query($sql,this.getCon());
  67. return mysql_num_row($rs);
  68. }
  69. /**********
  70. * close dbconnection;
  71. */
  72. public colse()
  73. {
  74. mysql_colse(this.getCon());
  75. self::DB = null;
  76. }
  77. }
  78. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn