Home >Backend Development >PHP Tutorial > _unset()这是如何了

_unset()这是如何了

WBOY
WBOYOriginal
2016-06-13 12:57:381007browse

__unset()这是怎么了

class Person {<br />
		private $name;<br />
		private $age;<br />
		private $sex;<br />
		//2. 构造方法		//作用: 给对象属性初始值<br />
		function __construct($name='', $age='0', $sex='男'){	<br />
			$this->name=$name;<br />
			$this->age=$age;<br />
			$this->sex=$sex;<br />
		}<br />
		private function __set($proName, $proValue){<br />
			if($proName == 'age'){<br />
				if($proValue<0 || $proValue>100){<br />
					echo  '设置的年龄不合法';<br />
					return;<br />
				}else{<br />
					 $this->$proName=$proValue;<br />
					echo $this->age.'<br />';<br />
				}<br />
			}<br />
		}<br />
		private function __get($proName){<br />
			if($proName == 'age'){<br />
				echo ($this->$proName-20).'<br />';<br />
			}<br />
		}<br />
		private function __isset($proName){<br />
			if(isset($this->$proName)){<br />
				echo $this->$proName.'存在<br />';<br />
				return;<br />
			}<br />
				echo $proName.'不存在<br />';<br />
		}<br />
		private function __unset($proName){<br />
			unset($this->$proName);<br />
		}<br />
		function say(){<br />
			echo 'name........'.$this->name.'|age.........'.$this->age.'|sex..........'.$this->sex.'<br />';<br />
		}<br />
		function __destruct(){<br />
		}<br />
	}<br />
	$p1 = new Person('yeteng', 26, '男');<br />
	$p2 = new Person('yeben', 28, '男');<br />
	$p3 = new Person('wenjing', 40, '女');<br />
	$p1->age=99;		//自动触发__set<br />
	$p1->age;				//自动触发__get<br />
	isset($p1->name);//自动触发__isset<br />
<br />
	unset($p1->age);	//自动触发__unset<br />
	$p1->say();


------解决方案--------------------
全部的魔术方法都必须是全局的(public)

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