Home >Backend Development >PHP Tutorial >The simple existence of php (one of the functions of the magic method: fault tolerance)

The simple existence of php (one of the functions of the magic method: fault tolerance)

WBOY
WBOYOriginal
2016-08-08 09:28:181027browse
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?php
// 重载
class Person{
	//定义属性
	public $name;
	private $age;

	//构造方法
	public function __construct($name,$age){
		$this->name =$name;
		$this->age  =$age;
	}

	//获取魔术方法
	# @param  string   $name
	public function __get($param){
		// return $this->$param;
		#允许被访问的属性
		$allow = array('age');
		if(in_array($param, $allow)){
			return $this->$param;
		}
	}


	//__set() 设置魔术方法
	#@param  string $name
	#param   string $val
	public function __set($name,$val){
		$allow = array('age','money');
		if(in_array($name, $allow)){
			$this->$name=$val;
		}
	}

	// __isset() 判定数据
	public function __isset($name){
		return isset($this->$name);
	}

	//__unset 销毁数据
	public function __unset($name){
		//建立unset列表
		$allow =array('tail');
		if(in_array($name,$allow)){
			echo 'unset running';
			unset($this->$name);
		}
	}
}

//实例化
$p = new Person('zp',21);
$p->age = 200;

echo $p->money = 200000000;
$p->tail ="a";

var_dump(isset($p->name));
var_dump(isset($p->age));
var_dump(isset($money));

// var_dump(empty($tail));
unset($p->tail);
// var_dump($tail);

</span>

The above has introduced the simple existence of PHP (one of the functions of the magic method: fault tolerance), including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn