Blogger Information
Blog 250
fans 3
comment 0
visits 321829
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
OOP 魔术方法 __isset与__unset
梁凯达的博客
Original
942 people have browsed it

实例

<?php

	//isset() 检测变量是否被声明 是返回true  否则返回false
	//unset() 销毁变量
	

	// $a = 100;
	// var_dump(isset($a));//
	// var_dump(isset($b));
	// unset($a);
	// var_dump(isset($a));
	

	class A{
		public $name = '看好屏幕别瞎动 我都能看到!';
		private $age = 18;
		private $sex = '男';
		//在没有权限使用isset函数来判断属性的时候自动调用
		public function __isset($a){
			//echo '我被调用了';
			//echo $a;
			return isset($this->$a);
		}
		//在没有权限使用unset函数销毁属性的时候自动调用
		public function __unset($b){
			echo '我被调用了';
			echo $b;
			unset($this->$b);
		}
	}

	$a = new A;
	var_dump(isset($a->name));
	var_dump(isset($a->age));
	if(isset($a->sex)){
		echo '存在';
	}else{
		echo '不存在';
	}
	echo '<hr/>';

	var_dump($a);
	//unset($a->name);
	unset($a->age);
	unset($a->sex);
	var_dump($a);

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post