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.迭代器
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";
}
结果是
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.工厂模式
//定义抽象类 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));
}
结果是
received updated received updated
//实例化时不会有任何输出,直到有一个事件或者一个参数被设置才有所行动,一开始有一个观察类。实现类继承观察类,实现类里面有改变值的类在初始化的时候实例一下。当有值改变时调用实现类继承观察类的方法(即完成通知)方法里面可以写被通知之后的操作,如打印字符串等等
5.观察者模式
**
给程序员一个鼓励呗!
**
- 微信
- 支付宝
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。