__set() __get() _isset() __unset() 在__unset() 在类中没有事先声明和已经声明过的属性调用unset的区别
<?php //echo strtr("I Love Mysql, Love PHP", "Mysql","MYSQL"); //$ar = array('Mysql'=>'MYSQL','PHP'=>'php'); //echo strtr("I Love Mysql, Love PHP", $ar); /* class myClass { public static function autoload($className) { if(is_file($className.".class.php")) { require_once($className.".class.php"); }else{ echo "{$className}类文件不存在!"; } } } spl_autoload_register(array('myClass','autoload')); $User = new User(); echo $User->getUserName(); echo "<BR><BR>";*/ class Base { private $UserName; private $Password; public function __get($key) { return $this->$key; } public function __set($key, $val) { echo "key=={$key},val=={$val}<BR><BR>"; $this->$key = $val; } public function __isset($key) { echo "<BR>===============call isset=============<BR>"; return isset($this->$key); } public function __unset($key) { echo "<BR>===============call __unset=============<BR>"; unset($this->$key); } } $base = new Base(); $base->UserName = "xlc"; $base->Password = "888888"; $base->age = 22; echo "UserName:{$base->UserName}, Password:{$base->Password},age:{$base->age}<BR><BR>"; echo "<BR><h1>age</h1><BR>"; unset($base->age); //直接在类中没有声明的属性,在unset时,并不会调用魔术方法__unset() if(isset($base->age)) //但__isset()是进行调用的 { echo "存在"; }else{ echo "不存在"; } echo "<BR>=======================<BR>"; echo "<BR><h1>Password</h1><BR>"; unset($base->Password); if(isset($base->Password)) //如果在类中定义了该属性,这里会调用两次__isset(); { echo "存在"; }else{ echo "不存在"; } echo "<BR><h1>UserName</h1><BR>"; unset($base->UserName); if(isset($base->UserName)) { echo "存在"; }else{ echo "不存在"; } echo "<BR>=======================<BR>"; //echo isset($base->$name) ? "property is setted.<br>" : "property is not setted.<br>"; /*$key = "Userame"; //当属性已经存在的情况下就不走__isset()了这是为何? //这里只有在$key为该对像已经定义的属性时才会调用__isset()魔术方法 if(isset($base->$key)) { echo "属性存在"; }else{ echo "属性不存在"; }*/ ?>