Home >Backend Development >PHP Tutorial >php设计模式 工厂、单例、注册树模式,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规范:
开发符合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);
魔术方法:
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、三种基本设计模式
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');