面向对象编程的基本原则:
1、设置类的属性
<span style="color: #0000ff;">class</span><span style="color: #000000;"> ShopProduct { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$title</span> = 'default product'<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$producterMainName</span> = 'main name'<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$producterFirstName</span> = 'first name'<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$price</span> = 0<span style="color: #000000;">; } </span><span style="color: #800080;">$product1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ShopProduct(); </span><span style="color: #008000;">//</span><span style="color: #008000;">设置属性</span> <span style="color: #800080;">$product1</span>->title = "My Antonia"<span style="color: #000000;">; </span><span style="color: #800080;">$product1</span>->producterFirstName = "Cather"<span style="color: #000000;">; </span><span style="color: #800080;">$product1</span>->producterMainName = "Willa"<span style="color: #000000;">; </span><span style="color: #800080;">$product1</span>->price = 5.99<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">访问</span> <span style="color: #0000ff;">echo</span> 'author: '.<span style="color: #800080;">$product1</span>->producterFirstName.' '.<span style="color: #800080;">$product1</span>->producterMainName;
用以上方法设置属性值会有很多问题:
第一:PHP允许动态设置属性,如果拼错或忘记属性名时并不会得到警告。例如错误地把
<span style="color: #800080;">$product1</span>->producterMainName = "Willa";
写作
<span style="color: #800080;">$product1</span>->producterSecondName = "Willa";
,当我们输出作者名字的时候,会有意想不到的结果。
第二:类太过松散,我们没有强制设置标题、价格或者产品名称,客户端代码可以确定的是这些属性存在,但是面对的可能是默认值也可能不是,理想情况下,我们希望实例化ShopProduct对象时设置有意义的属性值。
第三:不得不重复做一些经常做的事,比如多次需要完整输出作者的名字,就要重复使用
<span style="color: #0000ff;">echo</span> 'author: '.<span style="color: #800080;">$product1</span>->producterFirstName.' '.<span style="color: #800080;">$product1</span>->producterMainName;
改进后的:
2、使用方法
<span style="color: #0000ff;">class</span><span style="color: #000000;"> ShopProduct { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$title</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$producterMainName</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$producterFirstName</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$price</span> = 0<span style="color: #000000;">; <br> <span style="color: #008000;">//设置默认值,$this伪变量把类指向一个对象实例,$this可以理解为当前实例 </span></span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$title</span>, <span style="color: #800080;">$producterMainName</span>, <span style="color: #800080;">$producterFirstName</span>, <span style="color: #800080;">$price</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->title = <span style="color: #800080;">$title</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->producterMainName = <span style="color: #800080;">$producterMainName</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->producterFirstName = <span style="color: #800080;">$producterFirstName</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->price = <span style="color: #800080;">$price</span><span style="color: #000000;">; } <span style="color: #008000;">//获取作者名 </span></span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getProducter(){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->producterFirstName.' '.<span style="color: #800080;">$this</span>-><span style="color: #000000;">producterMainName; } } </span><span style="color: #800080;">$product1</span> = <span style="color: #0000ff;">new</span> ShopProduct('My Antonia', 'Willa', 'Cather', 5.99<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">访问</span> <span style="color: #0000ff;">echo</span> 'author: '.<span style="color: #800080;">$product1</span>->getProducter();
3、使用AccessArray实现配置文件的加载
ArrayAccess 是提供像访问数组一样访问对象的能力的接口。
Config.php
<span style="color: #000000;">php namespace Baobab; </span><span style="color: #0000ff;">class</span> Config <span style="color: #0000ff;">implements</span><span style="color: #000000;"> \ArrayAccess{ </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$path</span><span style="color: #000000;">; </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$configs</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @param $path 配置文件所在的文件系统目录 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$path</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->path = <span style="color: #800080;">$path</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span> offsetGet(<span style="color: #800080;">$key</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>->configs[<span style="color: #800080;">$key</span><span style="color: #000000;">])){ </span><span style="color: #800080;">$file_path</span> = <span style="color: #800080;">$this</span>->path.'/'.<span style="color: #800080;">$key</span>.'.php'<span style="color: #000000;">; </span><span style="color: #800080;">$config</span> = <span style="color: #0000ff;">require</span> <span style="color: #800080;">$file_path</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->configs[<span style="color: #800080;">$key</span>] = <span style="color: #800080;">$config</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span><span style="color: #800080;">$this</span>->configs[<span style="color: #800080;">$key</span><span style="color: #000000;">]; } </span><span style="color: #0000ff;">function</span> offsetSet(<span style="color: #800080;">$key</span>, <span style="color: #800080;">$value</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> \<span style="color: #0000ff;">Exception</span>("can't write config file"<span style="color: #000000;">); } </span><span style="color: #0000ff;">function</span> offsetExists(<span style="color: #800080;">$key</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->configs[<span style="color: #800080;">$key</span><span style="color: #000000;">]); } </span><span style="color: #0000ff;">function</span> offsetUnset(<span style="color: #800080;">$key</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$this</span>->configs[<span style="color: #800080;">$key</span><span style="color: #000000;">]); } }</span>
/Configs/database.php
<span style="color: #000000;">php </span><span style="color: #800080;">$config</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">( </span>'master' => <span style="color: #0000ff;">array</span><span style="color: #000000;">( </span>'type' => 'MySQL', 'host' => '127.0.0.1', 'user' => 'root', 'password' => 'root', 'dbname' => 'test'<span style="color: #000000;"> ) ); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$config</span>;
index.php
<span style="color: #800080;"><span style="font-style: italic;">define<span>(<span style="color: #008000; font-weight: bold;">'BASEDIR'<span>,<span style="color: #660e7a; font-weight: bold; font-style: italic;">__DIR__<span>);</span></span></span></span></span></span><br>$config</span> = <span style="color: #0000ff;">new</span> \Baobab\Config(BASEDIR.'/Configs'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">从文件系统加载一个配置文件Configs/database.php</span> <span style="color: #008080;">print_r</span>(<span style="color: #800080;">$config</span>['database']);