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

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

  •  
  •   beaaar · Oct 24, 2012 · 3438 views
    This topic created in 4944 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   ·   2957 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:48 · PVG 20:48 · LAX 05:48 · JFK 08:48
    ♥ Do have faith in what you're doing.