成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

php 語(yǔ)言特性學(xué)習(xí)(三)

EscapedDog / 2460人閱讀

摘要:當(dāng)有值改變時(shí)調(diào)用實(shí)現(xiàn)類繼承觀察類的方法即完成通知方法里面可以寫被通知之后的操作,如打印字符串等等觀察者模式給程序員一個(gè)鼓勵(lì)唄微信支付寶

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;
//結(jié)果是
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 函數(shù)使用

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);

結(jié)果是

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

結(jié)果是

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類   讀權(quán)限給,修改,刪除權(quán)限不給
abstract class User {
    private $name = null;
    function __construct($name) {
        $this->name = $name ;
    }
    function getName() {
        return $this->name;
    }
    //權(quán)限方法
    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 有修改權(quán)限
    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));
}

結(jié)果是

received updated received updated
//實(shí)例化時(shí)不會(huì)有任何輸出,直到有一個(gè)事件或者一個(gè)參數(shù)被設(shè)置才有所行動(dòng),一開始有一個(gè)觀察類。實(shí)現(xiàn)類繼承觀察類,實(shí)現(xiàn)類里面有改變值的類在初始化的時(shí)候?qū)嵗幌?。?dāng)有值改變時(shí)調(diào)用實(shí)現(xiàn)類繼承觀察類的方法(即完成通知)方法里面可以寫被通知之后的操作,如打印字符串等等

5.觀察者模式

**

給程序員一個(gè)鼓勵(lì)唄!

**

微信

支付寶

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/23154.html

相關(guān)文章

  • SegmentFault 技術(shù)周刊 Vol.40 - 2018,來學(xué)習(xí)一門新的編程語(yǔ)言吧!

    摘要:入門,第一個(gè)這是一門很新的語(yǔ)言,年前后正式公布,算起來是比較年輕的編程語(yǔ)言了,更重要的是它是面向程序員的函數(shù)式編程語(yǔ)言,它的代碼運(yùn)行在之上。它通過編輯類工具,帶來了先進(jìn)的編輯體驗(yàn),增強(qiáng)了語(yǔ)言服務(wù)。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不覺已經(jīng)到來了,總結(jié)過去的 2017,相信小伙們一定有很多收獲...

    caspar 評(píng)論0 收藏0
  • SegmentFault 技術(shù)周刊 Vol.40 - 2018,來學(xué)習(xí)一門新的編程語(yǔ)言吧!

    摘要:入門,第一個(gè)這是一門很新的語(yǔ)言,年前后正式公布,算起來是比較年輕的編程語(yǔ)言了,更重要的是它是面向程序員的函數(shù)式編程語(yǔ)言,它的代碼運(yùn)行在之上。它通過編輯類工具,帶來了先進(jìn)的編輯體驗(yàn),增強(qiáng)了語(yǔ)言服務(wù)。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不覺已經(jīng)到來了,總結(jié)過去的 2017,相信小伙們一定有很多收獲...

    nihao 評(píng)論0 收藏0
  • SegmentFault 技術(shù)周刊 Vol.40 - 2018,來學(xué)習(xí)一門新的編程語(yǔ)言吧!

    摘要:入門,第一個(gè)這是一門很新的語(yǔ)言,年前后正式公布,算起來是比較年輕的編程語(yǔ)言了,更重要的是它是面向程序員的函數(shù)式編程語(yǔ)言,它的代碼運(yùn)行在之上。它通過編輯類工具,帶來了先進(jìn)的編輯體驗(yàn),增強(qiáng)了語(yǔ)言服務(wù)。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不覺已經(jīng)到來了,總結(jié)過去的 2017,相信小伙們一定有很多收獲...

    Drummor 評(píng)論0 收藏0
  • 《簡(jiǎn)明 PHP 教程》01 關(guān)于 PHP

    摘要:名字背后的故事原本的簡(jiǎn)稱為,是拉斯姆斯勒多夫?yàn)榱司S護(hù)個(gè)人網(wǎng)頁(yè),而用語(yǔ)言開發(fā)的一些程序集。關(guān)于相互連接,已經(jīng)支持了對(duì)對(duì)象的即時(shí)連接,并且可以透明地將其用作對(duì)象。將所有的功能標(biāo)準(zhǔn)化于堅(jiān)實(shí)的擴(kuò)展,并且還增加了,以及支持以擴(kuò)充其功能。 PHP 是一種被廣泛應(yīng)用的開源通用計(jì)算機(jī)腳本語(yǔ)言,尤其適用于 Web 開發(fā)。PHP 的語(yǔ)法借鑒吸收 C 語(yǔ)言、Java 和 Perl 等流行計(jì)算機(jī)語(yǔ)言的特點(diǎn),易...

    2501207950 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<