资讯专栏INFORMATION COLUMN

php 语言特性学习(三)

EscapedDog / 2321人阅读

摘要:当有值改变时调用实现类继承观察类的方法即完成通知方法里面可以写被通知之后的操作,如打印字符串等等观察者模式给程序员一个鼓励呗微信支付宝

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
";
        }
    }

    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
";
        }
    }
}

$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
";
        }
    }
}

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 
"; }

结果是

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:
";
        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
";
    }
}

$login = array("andy","str","jack");
foreach($login as $key =>$val) {
    displayPermission(UserFactory::create($val));
}

结果是

received updated received updated
//实例化时不会有任何输出,直到有一个事件或者一个参数被设置才有所行动,一开始有一个观察类。实现类继承观察类,实现类里面有改变值的类在初始化的时候实例一下。当有值改变时调用实现类继承观察类的方法(即完成通知)方法里面可以写被通知之后的操作,如打印字符串等等

5.观察者模式

**

给程序员一个鼓励呗!

**

微信

支付宝

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/23154.html

相关文章

  • SegmentFault 技术周刊 Vol.40 - 2018,来学习一门新的编程语言吧!

    摘要:入门,第一个这是一门很新的语言,年前后正式公布,算起来是比较年轻的编程语言了,更重要的是它是面向程序员的函数式编程语言,它的代码运行在之上。它通过编辑类工具,带来了先进的编辑体验,增强了语言服务。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不觉已经到来了,总结过去的 2017,相信小伙们一定有很多收获...

    caspar 评论0 收藏0
  • SegmentFault 技术周刊 Vol.40 - 2018,来学习一门新的编程语言吧!

    摘要:入门,第一个这是一门很新的语言,年前后正式公布,算起来是比较年轻的编程语言了,更重要的是它是面向程序员的函数式编程语言,它的代码运行在之上。它通过编辑类工具,带来了先进的编辑体验,增强了语言服务。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不觉已经到来了,总结过去的 2017,相信小伙们一定有很多收获...

    nihao 评论0 收藏0
  • SegmentFault 技术周刊 Vol.40 - 2018,来学习一门新的编程语言吧!

    摘要:入门,第一个这是一门很新的语言,年前后正式公布,算起来是比较年轻的编程语言了,更重要的是它是面向程序员的函数式编程语言,它的代码运行在之上。它通过编辑类工具,带来了先进的编辑体验,增强了语言服务。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不觉已经到来了,总结过去的 2017,相信小伙们一定有很多收获...

    Drummor 评论0 收藏0
  • 《简明 PHP 教程》01 关于 PHP

    摘要:名字背后的故事原本的简称为,是拉斯姆斯勒多夫为了维护个人网页,而用语言开发的一些程序集。关于相互连接,已经支持了对对象的即时连接,并且可以透明地将其用作对象。将所有的功能标准化于坚实的扩展,并且还增加了,以及支持以扩充其功能。 PHP 是一种被广泛应用的开源通用计算机脚本语言,尤其适用于 Web 开发。PHP 的语法借鉴吸收 C 语言、Java 和 Perl 等流行计算机语言的特点,易...

    2501207950 评论0 收藏0
  • 全栈开发自学路线

    摘要:前言这里筑梦师是一名正在努力学习的开发工程师目前致力于全栈方向的学习希望可以和大家一起交流技术共同进步用简书记录下自己的学习历程个人学习方法分享本文目录更新说明目录学习方法学习态度全栈开发学习路线很长知识拓展很长在这里收取很多人的建议以后决 前言 这里筑梦师,是一名正在努力学习的iOS开发工程师,目前致力于全栈方向的学习,希望可以和大家一起交流技术,共同进步,用简书记录下自己的学习历程...

    wwolf 评论0 收藏0

发表评论

0条评论

EscapedDog

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<