V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
beaaar
V2EX  ›  PHP

关于类__unset(),__isset()的问题,__unset()掉的属性,通过__isset()检测会执行两次?

  •  
  •   beaaar · Oct 24, 2012 · 3432 views
    This topic created in 4943 days ago, the information mentioned may be changed or developed.
    <?php
    class Phone {
    private $band;
    private $color;
    private $name;

    function __construct($band = 'nokia', $color = 'black', $name = 'Lumia920') {
    $this->band = $band;
    $this->color = $color;
    $this->name = $name;
    }


    function __isset($property) {
    if( isset($this->$property) ) {
    echo $property.'is '.$this->$property.'.';
    }else {
    echo $property.' is not set.<br/>';
    }
    }
    function __unset($property) {
    unset($this->$property);
    echo 'Unset '.$property.' successfully.<br/>';
    }
    }

    $phone = new Phone('nokia','blue');
    unset($phone->name);
    echo '<hr/>';
    isset($phone->name);
    ?>
    输出结果
    Unset name successfully.
    --------------------------------------
    name is not set.
    name is not set.
    以上函数,unset掉一个属性后,isset查询该属性是否存在,则__isset()输出了两次;
    如果直接isset一个不存在的属性,就会正常执行,不明白是为什么?
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5657 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 09:03 · PVG 17:03 · LAX 02:03 · JFK 05:03
    ♥ Do have faith in what you're doing.