摘要:本文主要向大家介绍了PHP语言之设计模式—建造者模式,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
本文主要向大家介绍了PHP语言之设计模式—建造者模式,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
1、什么是建造者模式?
建造者设计模式定义了处理其他对象的复杂构建的对象设计。
2、用一个简单的mysql,mongo链接类说明:
[php] view plain copy print?
1. <?php
2. /**
3. * 数据库链接类 - 此类只是一个简单的说明实例,如需使用,请加以简单完善修改后在使用
4. * ------------------------
5. * @author liuxinming
6. * @Dtime:2012-07-02
7. */
8. class connect{
9. private $localhost;//主机 sql服务器
10. private $dbuser;//数据库用户名
11. private $password;//数据库密码
12. private $database;//数据库名
13. private $charset;//编码
14. private $pconnect;//是否持久链接
15.
16. private $port; //数据库端口
17. private $mongo;//mongo 对象
18. private $db;//db mongodb对象数据库
19.
20.
21. public function __construct($config){
22. $this->localhost=$config['localhost'];
23. $this->dbuser=$config['dbuser'];
24. $this->password=$config['password'];
25. $this->database=$config['database'];
26. $config['charset']?$this->charset=$config['charset']:'utf8';//默认为utf8字符集
27. $config['pconnect']?$this->pconnect=$config['pconnect']:0;//默认为utf8字符集
28. $config['port']?$this->port=$config['port']:3360;//端口
29.
30. //MongoDB
31. if (!$config['option']) $config['option'] = array('connect' => true);
32. }
33. /**
34. * MYSQL连接
35. * @return obj
36. */
37. public function db_connect(){
38. $conn = ($config['pconnect'] == 0) ? mysql_connect($this->localhost, $this->dbuser, $this->password, true) : mysql_pconnect($this->localhost, $this->dbuser, $this->password);
39. if(!$conn){
40. die('Could not connect: ' . mysql_error());
41. }
42. mysql_query('SET NAMES ' . $this->database, $conn); //设置数据库字符集
43. $dbname=@mysql_select_db($this->database, $conn);
44. if(!$dbname){
45. die ("Can\'t use test_db : " . mysql_error());
46. }
47. return $conn;
48. }
49. /**
50. * NoSql mongoDB链接
51. */
52. public function mongodb(){
53. $server = 'mongodb://' . $this->localhost . ':' . $this->port;
54. //链接mongodb 数据库
55. $this->mongo = new Mongo($server, $this->options);
56. //选择数据库
57. $this->db = $this->mongo->selectDB($this->database);
58. //用户名 密码
59. $this->db->authenticate($this->dbuser, $this->password);
60. //$mongo->connect(); //调用connect方法,来保持连接。
61. //$mongo->close(); //调用close方法,关闭数据库连接。
62. //$alldb= $this->mongo->listDBs(); //调用listDBs方法,返回$alldb这个多维数组。显示所有的数据库。
63. }
64. }
65. /**
66. * 建造者模式类 目的是处理数据库链接对象的复杂构建的对象设计
67. * 这只是一个简单说明类,如需分装一个完整的正式环境下用的数据库多种类型链接类,请稍微加以修改即可用。
68. * 当然此类你也可以直接拿去用,只是感觉不完美。
69. */
70. class MysqlDb{
71. protected $obj;
72. protected $sqltype;
73. public function __construct($config){
74. $this->obj=new connect($config);
75. $this->sqltype=$config['sqltype'];
76. }
77. public function buildDb(){
78. if($this->sqltype=='mysql'){
79. $this->obj->db_connect();
80. }else{
81. $this->obj->mongodb();
82. }
83. }
84. }
85. /* 创建过程被封装了,方便切换使用mysql 或 mongo数据库链接
86. */
87. $config = array(
88. 'sqltype'=>'mysql',
89. 'localhost' => '127.0.0.1',
90. 'dbuser' => 'root',
91. 'password' => '123456' ,
92. 'database'=>'weike',
93. );
94. $sql=new MysqlDb($config);
95. $sql->buildDb();
96.
97. // 用sql查询测试
98. $sql='select * from pre_brand';
99. $result =mysql_query($sql);
100. $row=mysql_fetch_array($result);
101. print_r($row);
102. mysql_free_result($result);//释放与之关联的资源 (脚本执行完毕后会自动释放内存)
103. ?>
测试输出结果:
Array ( [0] => 1 [bid] => 1 [1] => 9 [uid] => 9 [2] => 0 [like] => 0 [3] => [type] => [4] => 0 [local] => 0 [5] => 0 [date] => 0 [6] => 0 [extra1] => 0 [7] => 0 [extra2] => 0 [8] => 0 [extra3] => 0 [9] => 0 [extra4] => 0 [10] => 0.00 [extra5] => 0.00 [11] => 0.00 [extra6] => 0.00 [12] => 0.00 [extra7] => 0.00 [13] => 0.00 [extra8] => 0.00 )
PS:由于我的ubuntu服务器没有装MongoDB,所有没有测试mongodb。有安装的同志可以测试下。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号