ホームページ  >  記事  >  php教程  >  php设计模式 工厂、单例、注册树模式,php设计模式

php设计模式 工厂、单例、注册树模式,php设计模式

WBOY
WBOYオリジナル
2016-06-13 08:45:331708ブラウズ

php设计模式 工厂、单例、注册树模式,php设计模式

Source Code Pro字体  easyphp

命名空间:隔离类和函数,php5.3以后

//test5.php<br /><?<span>php
namespace Test5;<span>//命名空间必须是程序脚本的第一条语句,除了declare
<span>function<span> test(){
    <span>echo <span>__FILE__<span>;
}</span></span></span></span></span></span></span>
//test6.php<br /><?<span>php
namespace Test6;
<span>function<span> test(){
    <span>echo <span>__FILE__<span>;
}</span></span></span></span></span></span>
<span><span><?php<br />require 'test5.php'<span>;
<span>require 'test6.php'<span>;

Test5\test();
Test6\test();</span></span></span></span></span>

类自动载入:php5.2以后

spl_autoload_register('autoload1'<span>);

Test5::<span>test();
Test6::<span>test();

<span>function autoload1(<span>$class<span>){
    <span>require __DIR__.'/'.<span>$class.'.php'<span>;
}</span></span></span></span></span></span></span></span></span>

PSR-0规范

  • 命名空间必须与绝对路径一致
  • 类名首字母必须大写
  • 除入口文件外,其他“.php” 必须只有一个类。

开发符合PSR-0规范的基础框架

spl标准库

 

PHP链式操作

<?<span>php
namespace Baobab;

</span><span>class</span><span> Database{
    </span><span>function</span> where(<span>$where</span><span>){
        </span><span>return</span> <span>$this</span><span>;
    }
    
    </span><span>function</span> order(<span>$order</span><span>) {
        </span><span>return</span> <span>$this</span><span>;
    }
    
    </span><span>function</span> limit(<span>$limit</span><span>){
        </span><span>return</span> <span>$this</span><span>;
    }
}

</span>?><br /><br />//index.php<br />$db = new Baobab\Database();<br />$db->where('id = 1')->order('order by id')->limit(1);

魔术方法:

  • __get/__set:接管对象属性。在给不可访问属性赋值时,__set() 会被调用;读取不可访问属性的值时,__get() 会被调用。
  • __call/__callStatic:在对象中调用一个不可访问方法时,__call() 会被调用;用静态方式中调用一个不可访问方法时,__callStatic() 会被调用。
  • __toString:一个类转化成字符串
  • __invoke:以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用。

object.php

<?<span>php
namespace Baobab;

</span><span>class</span> <span>Object</span><span>{
    </span><span>protected</span> <span>$array</span> = <span>array</span><span>();
    </span><span>function</span> __set(<span>$key</span>, <span>$value</span><span>){
        </span><span>$this</span>-><span>array</span>[<span>$key</span>] = <span>$value</span><span>;
    }
    </span><span>function</span> __get(<span>$key</span><span>){
        </span><span>//</span><span>echo __METHOD__;</span>
        <span>return</span> <span>$this</span>-><span>array</span>[<span>$key</span><span>];
    }
    
    </span><span>function</span> __call(<span>$func</span>, <span>$param</span><span>){
        </span><span>//</span><span>var_dump($func,$param);</span>
        <span>return</span> 'magic function'<span>;
    }
    
    </span><span>static</span> <span>function</span> __callstatic(<span>$func</span>, <span>$param</span><span>) {
        <span>//</span></span><span>var_dump($func, $param);
        </span><span>return</span> 'magic static function'<span>;
    }
    
    </span><span>function</span><span> __toString(){
        </span><span>return</span> <span>__CLASS__</span><span>;
    }
    
    </span><span>function</span> __invoke(<span>$param</span><span>) {
        </span><span>return</span> <span>var_dump</span>(<span>$param</span><span>);
    }
    
}</span>

index.php

<span>$obj</span> = <span>new</span> baobab\<span>Object</span><span>();
</span><span>$obj</span>->title = 'hello'<span>;
</span><span>echo</span> <span>$obj</span>-><span>title;
</span><span>echo</span> <span>$obj</span>->test1('hello', 123<span>);
</span><span>echo</span> <span>$obj</span>::test1('hello1', 1234<span>);
</span><span>echo</span> <span>$obj</span><span>;
</span><span>echo</span> <span>$obj</span>('test1');

1、三种基本设计模式

  • 工厂模式:使用工厂方法或类生产对象,而不是在代码中直接new

Factory.php

<?<span>php
namespace Baobab;

</span><span>class</span><span> Factory{
    </span><span>static</span> <span>function</span><span> createDatabase(){
        </span><span>$db</span> = <span>new</span><span> Database();
        </span><span>return</span> <span>$db</span><span>;
    }
}</span>

index.php

<span>$db = Baobab\Factory::<span>createDatabase();<br /></span>$db1 = Baobab\Factory::<span>createDatabase();</span></span>
<span><span><span>$db->limit(<span>$limit);</span></span></span></span>
  • 单例模式:使某个类的对象仅允许创建一个

Database.php

<?<span>php
namespace Baobab;

</span><span>class</span><span> Database{
   
    </span><span>protected</span> <span>static</span> <span>$db</span><span>;
    
    </span><span>private</span> <span>function</span><span> __construct(){
        
    }
    
    </span><span>static</span> <span>function</span><span> getInstance(){
        </span><span>if</span> (self::<span>$db</span><span>){
            </span><span>return</span> self::<span>$db</span><span>;
        }</span><span>else</span><span>{
            </span><span>//</span><span>self是指向类本身,也就是self是不指向任何已经实例化的对象(::域运算符号)</span>
            self::<span>$db</span> = <span>new</span><span> self();
            </span><span>return</span> self::<span>$db</span><span>;
        }
    }</span>

index.php

<span>$db</span> = Baobab\Database::getInstance();
  • 注册模式:解决全局共享和交换对象,将对象注册到全局树上,可以在任何地方被直接访问

Register.php

<?<span>php
namespace Baobab;

</span><span>class</span><span> Register{
    </span><span>protected</span> <span>static</span> <span>$objects</span><span>;
    </span><span>static</span> <span>function</span> set(<span>$alias</span>, <span>$object</span><span>){
        self</span>::<span>$objects</span>[<span>$alias</span>] = <span>$object</span><span>;
    }
    
    </span><span>static</span> <span>function</span> _unset(<span>$alias</span><span>) {
        </span><span>unset</span>(self::<span>$objects</span>[<span>$alias</span><span>]);
    }
    
    </span><span>static</span> <span>function</span> get(<span>$name</span><span>) {
        </span><span>return</span> self::<span>$objects</span>[<span>$name</span><span>];
    }
}</span>
<span>//将db注册到注册树上</span>
Register::set('db1', <span>$db</span><span>);      </span>

index.php

<span>$db</span> = Baobab\Register::get('db1');

 

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。