Heim  >  Artikel  >  Backend-Entwicklung  >  php 的简单存在 (魔术方法作用之一:容错)_PHP教程

php 的简单存在 (魔术方法作用之一:容错)_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:02:43869Durchsuche

php 的简单存在 (魔术方法作用之一:容错)

<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(&#39;age&#39;);
		if(in_array($param, $allow)){
			return $this->$param;
		}
	}


	//__set() 设置魔术方法
	#@param  string $name
	#param   string $val
	public function __set($name,$val){
		$allow = array(&#39;age&#39;,&#39;money&#39;);
		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(&#39;tail&#39;);
		if(in_array($name,$allow)){
			echo &#39;unset running&#39;;
			unset($this->$name);
		}
	}
}

//实例化
$p = new Person(&#39;zp&#39;,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>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/969599.htmlTechArticlephp 的简单存在 (魔术方法作用之一:容错) name =$name;$this->age =$age;}//获取魔术方法# @param string $namepublic function __get($param){// return $this->$p...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn