php 语言特性学习(三)

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.观察者模式

**

给程序员一个鼓励呗!

**

  • 微信

微信

  1. 支付宝

支付宝

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值