Heim > php教程 > php手册 > Hauptteil

php面向对象编程 设计模式 - tianxintian22

WBOY
Freigeben: 2016-05-20 13:15:20
Original
1688 Leute haben es durchsucht

面向对象编程的基本原则:

  • 单一职责:一个类,只需要做好一件事
  • 开放封闭:一个类,应该是可扩展的,而不是可修改的
  • 依赖倒置:一个类,不应该强依赖另一个类。每个类对应另外一个类都是可替换的
  • 配置化:尽可能地使用配置,而不是硬编码
  • 面向接口编程:只需要关心接口,不需要关心实现

 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;
Nach dem Login kopieren

用以上方法设置属性值会有很多问题:

第一:PHP允许动态设置属性,如果拼错或忘记属性名时并不会得到警告。例如错误地把

<span style="color: #800080;">$product1</span>->producterMainName = "Willa";
Nach dem Login kopieren

写作

<span style="color: #800080;">$product1</span>->producterSecondName = "Willa";
Nach dem Login kopieren

,当我们输出作者名字的时候,会有意想不到的结果。

第二:类太过松散,我们没有强制设置标题、价格或者产品名称,客户端代码可以确定的是这些属性存在,但是面对的可能是默认值也可能不是,理想情况下,我们希望实例化ShopProduct对象时设置有意义的属性值。

第三:不得不重复做一些经常做的事,比如多次需要完整输出作者的名字,就要重复使用

<span style="color: #0000ff;">echo</span> 'author: '.<span style="color: #800080;">$product1</span>->producterFirstName.' '.<span style="color: #800080;">$product1</span>->producterMainName;
Nach dem Login kopieren

改进后的:

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();
Nach dem Login kopieren

 3、使用AccessArray实现配置文件的加载

ArrayAccess 是提供像访问数组一样访问对象的能力的接口。

Config.php
Nach dem Login kopieren
<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>
Nach dem Login kopieren

/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>;
Nach dem Login kopieren

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']);
Nach dem Login kopieren

 

Quelle:php.cn
Erklärung dieser Website
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
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage