Heim >Backend-Entwicklung >PHP-Tutorial >第一讲超市项目搭建(第138讲)

第一讲超市项目搭建(第138讲)

WBOY
WBOYOriginal
2016-06-13 12:28:151178Durchsuche

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

一:商城框架搭建示例图

 

 

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

              

 

 

三:代码部分

index.php代码

<span style="color: #008080;">1</span> <span style="color: #000000;">php</span><span style="color: #008080;">2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;">3</span> <span style="color: #008000;">用户直接访问的页面</span><span style="color: #008080;">4</span> <span style="color: #008000;">先加载框架初始化文件:init.php</span><span style="color: #008080;">5</span> <span style="color: #008000;">*/</span><span style="color: #008080;">6</span> <span style="color: #0000ff;">require</span>('./include/init.php'<span style="color: #000000;">);</span><span style="color: #008080;">7</span> <span style="color: #800080;">$conf</span> = conf::<span style="color: #000000;">getIns();</span><span style="color: #008080;">8</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$conf</span>->host."<br>";

init.php代码

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

db.class.php

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

conf.class.php代码

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

 

 

 
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:ThinkPHP框架1Nächster Artikel:好好写,好好作-PHP基础(二)