1.__get __set程序員
class Test { private $arr = array( 'x'=>null, 'y'=>null ); function __get($property) { if(array_key_exists($property,$this->arr)) { return $this->arr[$property]; } else { print "error:can not read this property is not exist other than x or y\n"; } } function __set($property,$value) { if(array_key_exists($property,$this->arr)) { $this->arr[$property] = $value; } else { print "error:can not write this property is not exist other than x or y\n"; } } } $cl = new Test(); $cl->x = 1; echo $cl->x; $cl->t = 2; echo $cl->t;
//結果是 1 error:can not write this property is not exist other than x or y error:can not read this property is not exist other than x or y
2.__call 函數使用微信
class HelloWorld { function display($time) { for($i=0;$i<$time;$i++) { print "hello world\n"; } } } class callHelloWorld { private $obj; function __construct() { $this->obj = new HelloWorld(); } function __call($method,$arg) { return call_user_func_array(array($this->obj,$method),$arg); } } $me = new callHelloWorld(); $me->display(3);
結果是函數
hello world hello world hello world
3.迭代器ui
class numberSquare implements Iterator { private $start; private $end; private $cur; public function __construct($start, $end) { $this->start = $start; $this->end = $end; } public function rewind() { $this->cur = $this->start; } public function key() { return $this->cur; } public function current() { return pow($this->cur,3); } public function next() { $this->cur++; } public function valid() { return $this->cur <= $this->end; } } $obj = new numberSquare(2,5); foreach($obj as $key => $value) { print "the square of $key is $value <br>\n"; }
結果是this
the square of 2 is 8 the square of 3 is 27 the square of 4 is 64 the square of 5 is 125
4.工廠模式spa
//定義抽象類 user類 讀權限給,修改,刪除權限不給 abstract class User { private $name = null; function __construct($name) { $this->name = $name ; } function getName() { return $this->name; } //權限方法 function hasReadPermission() { return true; } function hasModifyPermission() { return false; } function hasDeletePermission() { return false; } //定製方法 function wantsFlsahInterFace() { return true; } } class GuestUser extends User { } class CustomerUser extends User { //customer 有修改權限 function hasModifyPermission() { return true ; } }
class AdminUser extends User { function hasModifyPermission() { return true ; } function hasDeletePermission() { return true ; } function wantsFlsahInterFace() { return false; } } class UserFactory { private static $users = array( "andy"=>'Admin', "tom"=>'customer', "jack"=>'guest' ); static function create($name) { switch(self::$users[$name]) { case 'Admin' : return new AdminUser($name); break; case 'customer' : return new CustomerUser($name); break; case 'guest' : return new GuestUser($name); break; default: break; } } }
function boolToString($b) { if($b == true) { return "yes"; } else { return "no"; } } function displayPermission( $obj) { print $obj->getName() . "'s permission:\n"; print "Read: " . boolToString($obj->hasReadPermission()); print "Modify: " . boolToString($obj->hasModifyPermission()); print "Delete: " . boolToString($obj->hasDeletePermission()); } function displayRequirement( $obj) { if($obj->wantsFlsahInterFace()) { print $obj->getName() . "require flash\n"; } } $login = array("andy",'str','jack'); foreach($login as $key =>$val) { displayPermission(UserFactory::create($val)); }
結果是code
received updated received updated //實例化時不會有任何輸出,直到有一個事件或者一個參數被設置纔有所行動,一開始有一個觀察類。實現類繼承觀察類,實現類裏面有改變值的類在初始化的時候實例一下。當有值改變時調用實現類繼承觀察類的方法(即完成通知)方法裏面能夠寫被通知以後的操做,如打印字符串等等
5.觀察者模式繼承
**事件
**支付寶