__unset()魔术方法 删除类内私有属性

__unset()魔术方法 删除私有属性
unset()对共有属性进行删除 可通过__unset()魔术方法对私有属性进行操作

当在类外部执行unset()函数时,自动执行类内__unset()魔术方法

class hdw {
  private $name;
  private $age;
  private $money; //员工工资
  public $c;
  function __construct($name, $age = '', $money) {
    $this->name = $name;
    $this->age = $age;
    $this->money = $money;
  }
  function __isset($var) {
    $array = array ("name", "age" );
  if (in_array ( $var, $array )) {
    echo $var . "属性存在,他的值是:" . $this->$var;
  } elseif (in_array ( $var, array_keys ( get_object_vars ( $this ) ) )) {
    echo "属性不允许外部检查";
    return;
  } else {
    echo "属性不存在";
    }
  }
  function __unset($c) {
    if ($c == 'age') {
      unset ( $this->$c );
      echo "删除属性{$c}成功!";
    } else {
      echo "不允许删除属性{$c}";
    }
  }
  function get_money() {
    echo $this->money;
  }
}
$lisi = new hdw ( "李四", 22, 5500 );
//isset ( $lisi->money2222 ) ;
//echo isset($_GET['page'])?$_GET['page']:1;
unset ( $lisi->name );
//echo $lisi->get_money();

  

posted @ 2015-07-17 16:50  ITCHN  阅读(273)  评论(0编辑  收藏  举报