第一讲商城项目搭建(第138讲)

WBOY
发布: 2016-07-29 09:06:55
原创
773 人浏览过

一:商城框架搭建示例图

第一讲商城项目搭建(第138讲)

二:文件存放目录位置图片

第一讲商城项目搭建(第138讲)              第一讲商城项目搭建(第138讲)

三:代码部分

index.php代码

<span>1</span> <span>php
</span><span>2</span><span>/*</span><span>*
</span><span>3</span><span>用户直接访问的页面
</span><span>4</span><span>先加载框架初始化文件:init.php
</span><span>5</span><span>*/</span><span>6</span><span>require</span>('./include/init.php'<span>);
</span><span>7</span><span>$conf</span> = conf::<span>getIns();
</span><span>8</span><span>echo</span><span>$conf</span>->host."<br>";
登录后复制

init.php代码

<span> 1</span> <span>php
</span><span> 2</span><span>/*</span><span>*
</span><span> 3</span><span>file init.php
</span><span> 4</span><span>作用:框架初始化
</span><span> 5</span><span>*/</span><span> 6</span><span>/*</span><span>过滤参数,用递归的方式过滤$_GET,$_POST,$_COOKIE. 暂时放下</span><span>*/</span><span> 7</span><span>/*</span><span>初始化当前的绝对路径</span><span>*/</span><span> 8</span><span>//</span><span>换成正斜线是因为  win/linux 都支持正斜线,而linux不支持反斜线。也就是兼容性问题</span><span> 9</span><span>define</span>('ROOT', <span>str_replace</span>("\\", "/", <span>dirname</span>(<span>__FILE__</span>)) . '/'<span>);
</span><span>10</span><span>//</span><span>引入数据库类和conf类文件</span><span>11</span><span>require</span>(ROOT . 'db.class.php'<span>);
</span><span>12</span><span>require</span>(ROOT . 'conf.class.php'<span>);
</span><span>13</span><span>/*</span><span>设置报告错误级别</span><span>*/</span><span>14</span><span>define</span>("DEBUG",<span>true</span><span>);
</span><span>15</span><span>//</span><span>若果DEBUG为真,说明我们开启调试模式</span><span>16</span><span>if</span>( <span>defined</span>('DEBUG'<span>) ){
</span><span>17</span><span>error_reporting</span>(<span>E_ALL</span><span>);
</span><span>18</span> }<span>else</span><span>{
</span><span>19</span><span>//</span><span>否则说明,我们的文件是在服务器上供用户访问,则不报错,避免安全隐患问题</span><span>20</span><span>error_reporting</span>(0<span>);
</span><span>21</span> }
登录后复制

db.class.php

<span> 1</span> <span>php
</span><span> 2</span><span>/*</span><span>*
</span><span> 3</span><span>file db.class.php
</span><span> 4</span><span>作用: 数据库类
</span><span> 5</span><span>因为目前不知道使用什么数据库,因此我们先来写个接口
</span><span> 6</span><span>*/</span><span> 7</span><span>abstract</span><span>class</span><span> db{
</span><span> 8</span><span>/*</span><span> 9</span><span>    连接服务器
</span><span>10</span><span>    parms $h 服务器地址
</span><span>11</span><span>    parms $u 用户名
</span><span>12</span><span>    parms $p 密码
</span><span>13</span><span>    return bool
</span><span>14</span><span>*/</span><span>15</span><span>public</span><span>abstract</span><span>function</span> connect(<span>$h</span>,<span>$u</span>,<span>$p</span><span>);
</span><span>16</span><span>/*</span><span>17</span><span>    发送查询
</span><span>18</span><span>    parms $sql 发送的sql语句
</span><span>19</span><span>    return mixed bool/resource
</span><span>20</span><span>*/</span><span>21</span><span>public</span><span>abstract</span><span>function</span> query(<span>$sql</span><span>);
</span><span>22</span><span>/*</span><span>23</span><span>    查询多行数据
</span><span>24</span><span>    parms $sql select型语句
</span><span>25</span><span>    return array/false
</span><span>26</span><span>*/</span><span>27</span><span>public</span><span>abstract</span><span>function</span> getAll(<span>$sql</span><span>);
</span><span>28</span><span>/*</span><span>29</span><span>    查询单行数据
</span><span>30</span><span>    parms $sql select型语句
</span><span>31</span><span>    return array/false
</span><span>32</span><span>*/</span><span>33</span><span>public</span><span>abstract</span><span>function</span> getRow(<span>$sql</span><span>);
</span><span>34</span><span>/*</span><span>35</span><span>    查询单个数据
</span><span>36</span><span>    parms $sql select型语句
</span><span>37</span><span>    return array/false
</span><span>38</span><span>*/</span><span>39</span><span>public</span><span>abstract</span><span>function</span> getOne(<span>$sql</span><span>);
</span><span>40</span><span>/*</span><span>41</span><span>    自动执行insert/update语句
</span><span>42</span><span>    parms $sql select型语句
</span><span>43</span><span>    return array/false
</span><span>44</span><span>    例如:
</span><span>45</span><span>    $this->autoExecute('user',array('username'=>'zs', 'email'=>'zs@qq.com', 'insert'));
</span><span>46</span><span>    将自动转成:insert into user(username,email) values ('zs', 'zs@qq.com');
</span><span>47</span><span>*/</span><span>48</span><span>public</span><span>abstract</span><span>function</span> autoExecute(<span>$table</span>,<span>$data</span>,<span>$act</span>='insert',<span>$where</span><span>);
</span><span>49</span> }
登录后复制

conf.class.php代码

<span> 1</span> <span>php
</span><span> 2</span><span>/*</span><span>*
</span><span> 3</span><span>file conf.class.php
</span><span> 4</span><span>作用:配置文件读取类
</span><span> 5</span><span>*/</span><span> 6</span><span>class</span><span> conf{
</span><span> 7</span><span>//</span><span>静态属性用来存放本类实例化的对象</span><span> 8</span><span>protected</span><span>static</span><span>$ins</span> = <span>null</span><span>;
</span><span> 9</span><span>//</span><span>保存读取过来的配置文件信息</span><span>10</span><span>protected</span><span>$data</span> = <span>array</span><span>();
</span><span>11</span><span>//</span><span>将conf类的构造函数保护起来,且不让继承(防止改写)</span><span>12</span><span>final</span><span>protected</span><span>function</span><span> __construct(){
</span><span>13</span><span>//</span><span>一次性把配置文件信息读过来,赋给data属性,这样以后就不用管配置文件
</span><span>14</span><span>        //再找配置文件信息直接找$data就可以了</span><span>15</span><span>include</span>(ROOT . 'config.inc.php'<span>);
</span><span>16</span><span>$this</span>->data = <span>$_CFG</span><span>;
</span><span>17</span><span>    }
</span><span>18</span><span>final</span><span>protected</span><span>function</span><span> __clone(){
</span><span>19</span><span>    }
</span><span>20</span><span>//</span><span>单例模式</span><span>21</span><span>public</span><span>static</span><span>function</span><span> getIns(){
</span><span>22</span><span>if</span>(self::<span>$ins</span><span> instanceof self){
</span><span>23</span><span>return</span> self::<span>$ins</span><span>;
</span><span>24</span>         }<span>else</span><span>{
</span><span>25</span>             self::<span>$ins</span> = <span>new</span><span> self();
</span><span>26</span><span>return</span> self::<span>$ins</span><span>;
</span><span>27</span><span>        }
</span><span>28</span><span>    }
</span><span>29</span><span>//</span><span>用魔术方法,读取data属性内的信息</span><span>30</span><span>public</span><span>function</span> __get(<span>$key</span><span>){
</span><span>31</span><span>if</span>(<span>array_key_exists</span>(<span>$key</span>, <span>$this</span>-><span>data)){
</span><span>32</span><span>return</span><span>$this</span>->data[<span>$key</span><span>];
</span><span>33</span>         }<span>else</span><span>{
</span><span>34</span><span>return</span><span>null</span><span>;
</span><span>35</span><span>        }
</span><span>36</span><span>    }
</span><span>37</span><span>//</span><span>在运行期间,动态增加或改变配置选项</span><span>38</span><span>public</span><span>function</span> __set(<span>$key</span>, <span>$value</span><span>){
</span><span>39</span><span>$this</span>->data[<span>$key</span>] = <span>$value</span><span>;
</span><span>40</span><span>    }
</span><span>41</span><span>}
</span><span>42</span><span>/*</span><span>43</span><span>//测试代码
</span><span>44</span><span>//测试是否可以创建单例模式对象出来
</span><span>45</span><span>$conf = conf::getIns();
</span><span>46</span><span>//读取选项
</span><span>47</span><span>echo $conf->host."<br>";
</span><span>48</span><span>echo $conf->user."<br>";
</span><span>49</span><span>//动态追加选项
</span><span>50</span><span>$conf->template_dir = "D:\LAMP\Apache2.2\htdocs\boolshop\include";
</span><span>51</span><span>echo $conf->template_dir;
</span><span>52</span><span>*/</span><span>53</span> config.inc.<span>php代码
</span><span>54</span> <span>php
</span><span>55</span><span>/*</span><span>*
</span><span>56</span><span>file config.inc.php
</span><span>57</span><span>作用:配置文件
</span><span>58</span><span>*/</span><span>59</span><span>$_CFG</span> = <span>array</span><span>();
</span><span>60</span><span>$_CFG</span>['host'] = 'localhost'<span>;
</span><span>61</span><span>$_CFG</span>['user'] = 'root'<span>;
</span><span>62</span><span>$_CFG</span>['pwd'] = '123456';
登录后复制

 

以上就介绍了第一讲商城项目搭建(第138讲),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板