邵珠庆の博客 生命只有一次,你可以用它来做些更多伟大的事情–Make the world a little better and easier

287月/113

PHP类中的魔术方法+动态类导学

PHP不知道为什么,钟爱“魔术”这个词,什么都弄魔术。其实想想也非常形象,就拿PHP中的魔术方法来说吧,你不需要去调用
他们的任何一个方法,他们却可以执行,影响你的程序。

在学习之前我要说两句,首先你不能漠视魔术方法,不要认为你不用这世界上用的人就非常少,作用就小。其实 PHP魔术方法PHP的一大特征,既然你学了一种语言,连它最基本的特征都没学,还叫学习了这个语言了吗?其次,不要认为看完我这篇文章就可以深入的 理解魔术方法了,我只是带您入门,以后的路还得您自己走。最后,腾讯曾经出了个面试题,就是关于魔术方法的,如果你学习完我这篇文章,应付这道题应该不成 问题了。还有一点,学习魔术方法一个最重要的地方就是学习它的用法,也就是它在什么情况下被调用。这是重点中的重点,请在下面的学习中注意这点。

首先学习PHP4中就有的魔术方法——__sleep()和__wakeup()。

其实这两个方法用的非常非常的少,少到在我接触PHP1年半的前提下,如果不去主动学习他们根本不知道他们的存在,但是本着对
学术严谨的态度,多学点没什么坏处,用不了多少时间,所以你也可以跟着我花几分钟的时间熟悉一下这两个方法。

我刚才说他们用的非常非常的少,我用了两个非常,大家可以想想一下他们的程度。不光我们要知道他们用的少,重要的是知道为
什么用的少。我先写一段代码,你看看你是否见到过或者曾经写过,我见识比较少,至少我之前是没有见过这么写的。

<br />
		class Test {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $testStr;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function fun() {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //搞点小事情<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
		}<br />
		$test = new Test();<br />
		echo serialize($test);&nbsp;&nbsp;//输出&nbsp;&nbsp;O:4:&quot;Test&quot;:1:{s:7:&quot;testStr&quot;;N;}<br />
		

它竟然把一个的给序列化了,也就是把一个转换成了一个字符串,可以传输或者保存下来。我以前可没有接触过这么神奇的事情。

下面我修改一下上面的代码,上面不是要搞点小事情吗?那我现在就搞给你看!

<br />
		class Test {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $testStr;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __construct($str) {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;testStr = $str;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
		}<br />
		$test = new Test(&quot;Skiyo.cn&quot;);<br />
		echo serialize($test);&nbsp;&nbsp;//输出&nbsp; &nbsp;O:4:&quot;Test&quot;:1:{s:7:&quot;testStr&quot;;s:8:&quot;Skiyo.cn&quot;;}<br />
		

大家可以看到,$testStr被设置后,序列化后也对应了相应的值,但是现在有个问题,比如我这个变量是个秘密呢?而且我又得把这个序列化传给别的地方呢?
看下面的代码

<br />
		class Test {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $mySecret;&nbsp;&nbsp;//我的秘密不想让人知道<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __construct($secret) {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;mySecret = $secret;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
		}<br />
		$test = new Test(&quot;我的心里话&nbsp;&nbsp;我爱某某某&quot;);<br />
		echo serialize($test);&nbsp;&nbsp;//输出&nbsp;&nbsp;O:4:&quot;Test&quot;:1:{s:8:&quot;mySecret&quot;;s:32:&quot;我的心里话&nbsp;&nbsp;我爱某某某&quot;;}<br />
		

大家可以看到,我的秘密序列化后还是存在的,可是我不想我的心里话被别人看到。这个时候PHP很贴心,她知道你的问题,所以设置了魔术方法

__sleep() 就表示当你执行serialize()这个序列化函数之前时的事情,就像一个回调函数,所以在这个回调函数里面我们就可以做点事情,来隐藏我的秘密。

<br />
		class Test {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $mySecret;&nbsp;&nbsp;//我的秘密不想让人知道<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __construct($secret) {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;mySecret = $secret;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __sleep() {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;mySecret = &quot;你休想知道我的秘密!&quot;;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return array(&#39;mySecret&#39;);&nbsp;&nbsp;//一定要返回变量,不然返回的是个空,所以序列化也就没有东西了。<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
		}<br />
		$test = new Test(&quot;我的心里话&nbsp;&nbsp;我爱某某某&quot;);<br />
		echo serialize($test);&nbsp;&nbsp;//输出&nbsp;&nbsp;O:4:&quot;Test&quot;:1:{s:8:&quot;mySecret&quot;;s:28:&quot;你休想知道我的秘密!&quot;;}<br />
		

大家看到了吧?我的心里话被加密了,这个就是__sleep()的作用。至于__wakeup()和__sleep()大同小异,只不过是反序列化之前进行的回调函数。我不详细说了,大家看下下面的代码就明白了。

<br />
		class Test {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $mySecret;&nbsp;&nbsp;//我的秘密不想让人知道<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __construct($secret) {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;mySecret = $secret;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __sleep() {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;mySecret = &quot;你休想知道我的秘密!&quot;;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return array(&#39;mySecret&#39;);&nbsp;&nbsp;//一定要返回变量,不然返回的是个空,所以序列化也就没有东西了。<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __wakeup() {<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;mySecret = &quot;我的秘密又回来了,哈哈!&quot;;<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //反序列化就不用返回数组了,就是对字符串的解密,字符串已经有了不用其他的了。<br />
		&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
		}<br />
		$test = new Test(&quot;我的心里话&nbsp;&nbsp;我爱某某某&quot;);<br />
		print_r(unserialize(serialize($test)));&nbsp;&nbsp;//输出&nbsp;&nbsp;Test Object ( [mySecret] =&gt; 我的秘密又回来了,哈哈! )<br />
		

到此为止我们的__sleep()和__wakeup()两个魔术函数就学习完毕了。

即使你曾经对魔术方法没有任何接触,经过上节课的学习,那么我相信你也对魔术方法有一定的了解了。有了上节课的基础,我们的学习将会非常简单,因为魔术方法都是大同小异的东西。

今天我们要学习的是四个非常有用的魔术方法:__set() __get() __isset() __unset() ,特别是其中的 __set() 和 __get() ,在以后的面向对象的编程中经常会遇到,所以也是我们今天的重点。

我们还是从一个代码的例子出手。我们先来按照Java中的思想写一个

<br />
			class Test {<br />
			<br />
			&nbsp; &nbsp; //私有属性<br />
			<br />
			&nbsp; &nbsp; private $a;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $b;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $c;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $d;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $e;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $f;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $g;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $h;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $i;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $j;<br />
			&nbsp; &nbsp; //还有很多很多....<br />
			<br />
			&nbsp; &nbsp; //Getters &amp; Setters<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getA() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;a;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getB() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;b;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getC() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;c;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getD() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;d;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getE() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;e;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getF() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;f;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getG() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;g;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getH() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;h;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getI() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;i;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function getJ() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;j;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setA($a) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;a = $a;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setB($b) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;b = $b;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setC($c) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;c = $c;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setD($d) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;d = $d;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setE($e) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;e = $e;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setF($f) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;f = $f;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setG($g) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;g = $g;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setH($h) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;h = $h;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setI($i) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;i = $i;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function setJ($j) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;j = $j;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; //还有很多很多...<br />
			}<br />
			

请不要认为我这是在凑字数,因为你得相信我,我没有稿费,所以不用故意拖这么多字数。

在一般的面向对象的编程中,这种情况是相当常见的,我曾经用Struts2写过一个音乐分享的网站,单单一个注册,因为注册要获取的字段很多,似PHP 的$_POST,Struts2的机制就是给每个Form元素都设定一个Getter和Setter,这样下来,单单一个注册的就有多的数不清的 Getters和Setters。这是个非常艰巨的任务,程序员是写程序的,不是抄程序的,所以强大的Eclipse就为我们提供了一个简单的方法,只要 你设定了的属性,你就可以通过鼠标点击来生成相应的Getter和Setter,所以你不要认为上面的代码是我傻乎乎写的,我是个懒人,十分会偷懒 的:)

扯远了,PHP为了解决这个问题,而且又有魔术方法的基础,所以PHP创造了 __set() 和 __get() 。你只需要写一个方法,就可以代表上面大部分的Getters和Setters。

<br />
			class Test {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $a;<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __set($key, $value) {&nbsp;&nbsp;//必须是两个参数,第一个是属性,第二个是设置属性的值。<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;$key = $value;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //这里不明白为什么是$this-&gt;$key ?? 一般不是这样写吗? $this-&gt;key ? 请看我下面的分解。<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __get($key) {&nbsp;&nbsp;//一个参数,要获得的属性<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;$key;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			}<br />
			$test = new Test();<br />
			$test-&gt;__set(&#39;a&#39;, &#39;我是A&#39;);&nbsp;&nbsp;//给 属性a 设置一个值<br />
			echo $test-&gt;__get(&#39;a&#39;);&nbsp;&nbsp;//打印a&nbsp; &nbsp;输出&nbsp;&nbsp;我是A<br />
			

通过上面的例子,我们就给A设置了一个值,并且打印了出来。

现在我来说上面为什么是$this->$key。这要说明白也是个不小的话题,PHP比较神奇,她可以设置一个变量的变量,什么意思呢?来看下面的例子。

<br />
			$a = &#39;b&#39;;<br />
			$b = &#39;c&#39;;<br />
			echo $$a;&nbsp;&nbsp;//输出c<br />
			

哇好神奇,竟然有连个$,好多的钱啊。下面我给你分解下。

<br />
			$a = &#39;b&#39;;<br />
			$b = &#39;c&#39;;<br />
			echo ${$a};&nbsp;&nbsp;//这样看的话是不是就明白多了呢? $a=&#39;b&#39;,所以大括号中就是b了,加上前面的$,就变成变量$b了。<br />
			

回到上面的类中,你可以注意到,$key只是我们需要传进来的一个内部属性,不是我们真正需要的,如果$this->key这样就会编程$key这 个属性的值了,而我们需要设置的是$this->a的值,所以这里就需要使用变量的变量了。明白了吗?有点绕,好好想想:)

经过这么折腾,我想你已经会一点 __get() 和 __set() 的使用方法了。但是上面的代码还不能用于实际的代码中,因为在实践中,往往内部属性的个数是不定的,你想到了什么解决办法?对,就是数组,PHP中数组的 长度是不定的,所以我们就可以给他无限增加元素。

<br />
			class Test {<br />
			&nbsp; &nbsp;public $values = array();&nbsp;&nbsp;//存放属性的数组,以键值对的形式存在的。<br />
			<br />
			&nbsp; &nbsp;public function __get($name){<br />
			&nbsp; &nbsp;&nbsp; &nbsp; return $this-&gt;values[$name];<br />
			&nbsp; &nbsp;}<br />
			<br />
			&nbsp; &nbsp;public function __set($name, $value){<br />
			&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;values[$name] = $value;<br />
			&nbsp; &nbsp;}<br />
			}<br />
			$test = new Test();<br />
			$test-&gt;__set(&#39;a&#39;, &#39;我是a&#39;);<br />
			$test-&gt;__set(&#39;b&#39;, &#39;我是b&#39;);<br />
			echo $test-&gt;__get(&#39;a&#39;);&nbsp;&nbsp;//输出&nbsp;&nbsp;我是a<br />
			echo $test-&gt;__get(&#39;b&#39;);&nbsp;&nbsp;//输出&nbsp;&nbsp;我是b<br />
			

这样的话,如果是注册的页面,我们就可以灵活的获得和设置属性了。但是在实际运用中,我们知道传过来的值是不可靠的,我们得需要HTML转义,Mysql转义等等,所以我们可以写一个简单的回调函数,做数据验证的作用。

<br />
			class Test {<br />
			&nbsp; &nbsp;public $values = array();<br />
			<br />
			&nbsp; &nbsp;public function __get($name){<br />
			&nbsp; &nbsp;&nbsp; &nbsp; return $this-&gt;values[$name];<br />
			&nbsp; &nbsp;}<br />
			<br />
			&nbsp; &nbsp;public function __set($name, $value){<br />
			&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;values[$name] = $this-&gt;validate($value);<br />
			&nbsp; &nbsp;}<br />
			&nbsp; &nbsp;private function validate($value){<br />
			&nbsp; &nbsp;&nbsp; &nbsp; return htmlspecialchars(addslashes($value));<br />
			&nbsp; &nbsp;&nbsp; &nbsp; //等等<br />
			&nbsp; &nbsp;}<br />
			}<br />
			$test = new Test();<br />
			$test-&gt;__set(&#39;a&#39;, &#39;&lt;a&gt;我是a&lt;/a&gt;&#39;);<br />
			echo $test-&gt;__get(&#39;a&#39;);&nbsp;&nbsp;//输出&nbsp;&nbsp;&amp;lt;a&amp;gt;我是a&amp;lt;/a&amp;gt; 被转义啦...<br />
			

但是有的人不需要这样的,比如我前面提交的页面就有用户名和密码,不想再要别的了。当然,你如果用两个私有属性也可以解决,但是我说一下用一个数组的方法。

<br />
			class Test {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public $values = array(&#39;name&#39; =&gt; &#39;名字&#39;, &#39;passwd&#39; =&gt; &#39;密码&#39;);&nbsp;&nbsp;//提前规定私有属性只有name和passwd<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __get($name){<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($this-&gt;values[$name])) {&nbsp;&nbsp;//判断一下<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;values[$name];<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#39;没有此属性!&#39;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __set($name, $value){<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($this-&gt;values[$name])) {&nbsp;&nbsp;//这里也判断一下<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;values[$name] = $this-&gt;validate($value);<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#39;没有此属性!&#39;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private function validate($value){<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return htmlspecialchars(addslashes($value));<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //等等<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			}<br />
			$test = new Test();<br />
			$test-&gt;__set(&#39;name&#39;, &#39;&lt;a&gt;我是name&lt;/a&gt;&#39;);&nbsp;&nbsp;//设置name 无输出<br />
			$test-&gt;__set(&#39;我也不知道这个是什么&#39;, &#39;&lt;a&gt;看看是我的值是多少&lt;/a&gt;&#39;);&nbsp;&nbsp;//输出 没有此属性!<br />
			echo $test-&gt;__get(&#39;name&#39;);&nbsp;&nbsp;//输出&nbsp;&nbsp;&amp;lt;a&amp;gt;我是a&amp;lt;/a&amp;gt;<br />
			echo $test-&gt;__get(&#39;随便看看&#39;);&nbsp;&nbsp;//输出 没有此属性!<br />
			

到这里,我们算是基本掌握了 __set() 和 __get() 了,其实要学精通还是需要你自己去努力的,我只是带你入门而已。下面我们来看看 __isset() 和 __unset() 。

其实__isset() 和 __unset() 与 __sleep()和__wakeup() 是有点像的,__isset() 和 __unset() 是在触发isset()和unset()函数前的回调函数。我们继续来做例子。

<br />
			class Test {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public $a;<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __construct($value) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;a = $value;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __isset($name) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &quot;你小子偷看我??&quot;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __unset($name) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &quot;你没权利宰了我!&quot;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			}<br />
			$test = new Test(&quot;我是a&quot;);<br />
			isset($test-&gt;a); //无任何输出<br />
			

执行上面的代码,结果无任何输出,但是刚才我说了,只要执行isset()就要执行__isset()这个回调函数的啊,回调函数里面输出 “你小子偷看我??” ,但是为什么结果却没有输出呢?难道是我说错了??我不能欺骗大家啊,所以我得给大家说清楚,不是我错了,也不是程序错了,是PHP的问题!

在PHP中关于 __isset() 和 __unset() 有个约定,就是当用 isset() 函数测试成员属性的时候,如果这个属性为public属性或者默认属性时,就不回调,当为protected或者private时就回调。我语言表达能力 差,还是用代码表示吧。

<br />
			class Test {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; var $a;&nbsp;&nbsp;//这样就不输出<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public $a;&nbsp;&nbsp;//这样也不输出<br />
			<br />
			&nbsp; &nbsp; protected $a;&nbsp;&nbsp;//输出 你小子偷看我??<br />
			&nbsp; &nbsp; private $a;&nbsp;&nbsp;//输出 你小子偷看我??<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __construct($value) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;a = $value;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __isset($name) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &quot;你小子偷看我??&quot;;<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return isset($this-&gt;a);&nbsp;&nbsp;//返回真是的信息<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __unset($name) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &quot;你没权利宰了我!&quot;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			}<br />
			$test = new Test(&quot;我是a&quot;);<br />
			isset($test-&gt;a);<br />
			

这样明白了吧?__unset()是一样的,这两个函数其实也有大作用,比如你做一个网游(我只是比喻,现实不是这样的),有个怪物类中有个属性是怪物的 血,你不能想unset()就unset(),也就是说你不能等怪物的血没完了就使他死了。我还用例子说明。这个例子我自认为相当的好,自恋一下:)把上 面的内容都囊括了,希望你可以好好执行下,分析下,为什么会出这样的结果。

<br />
			class Monster {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 怪物的属性表<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @var array<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private $state = array(&#39;hp&#39; =&gt; 100, &#39;mp&#39; =&gt; 100);<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 打击怪物<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @access public<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @return void<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function hit() {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;state[&#39;hp&#39;] = $this-&gt;state[&#39;hp&#39;] - rand(1, 10);<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 设置怪物属性<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param string $key<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param integer $value<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @access public<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @return void<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __set($key, $value) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($this-&gt;state[$key])) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;state[$key] = $this-&gt;validate($value);<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#39;没有此属性!&#39;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 获得怪物属性<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param string $key<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @access public<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @return integer<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __get($key) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($this-&gt;state[$key])) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;state[$key];<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#39;没有此属性!&#39;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 验证设定怪物的血是否合法<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param integer $value<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @access private<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @return void<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
			&nbsp; &nbsp; &nbsp; &nbsp; private function validate($value){<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (is_int($value) &amp;&amp; $value &gt; 0) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $value;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 查看是否存在怪物的状态<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param string $key<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @return boolean<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __isset($key) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return isset($this-&gt;state[$key]);<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 查看怪物的属性是否为0<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param string $key<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @access public<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @return void<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function __unset($key) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($this-&gt;state[$key] &gt; 0) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#39;你没权利宰了我!&#39;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($this-&gt;state[$key]);<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#39;我死了!&#39;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			}<br />
			<br />
			$monster = new Monster();<br />
			$monster-&gt;__set(&#39;hp&#39;, 100);&nbsp;&nbsp;//设定怪物的血和蓝<br />
			$monster-&gt;__set(&#39;mp&#39;, 100);<br />
			$monster-&gt;__set(&#39;血&#39;, 200);&nbsp; &nbsp;//输出 没有此属性!<br />
			while (isset($monster-&gt;hp)) {&nbsp;&nbsp;//当怪物还存在的时候<br />
			&nbsp; &nbsp; &nbsp; &nbsp; $monster-&gt;hit();&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//开始打怪<br />
			&nbsp; &nbsp; &nbsp; &nbsp; unset($monster-&gt;hp);&nbsp; &nbsp;&nbsp; &nbsp; //消灭怪物<br />
			&nbsp; &nbsp; &nbsp; &nbsp; echo $monster-&gt;__get(&#39;hp&#39;);&nbsp;&nbsp;//输出怪物剩余血量 当怪物死后输出 没有此属性!<br />
			}<br />
			

写网游一直是儿时的梦想,但是现在比较现实了,根本不可能,所以我就在这里写个虚假的类来YY下吧,大家不要BS我:)

好了,今天的笔记到此为止,明天我们将要学习 __call() 。构造函数和析构函数我不准备说了,思想不是PHP里特有的,看些OOP的教程都有讲。
明天要毕业答辩,不知道笔记会不会按时发布,请期待。

 

昨天答辩实在曲折,到现在还没有开始,废话不说了,感觉继续奉上教程笔记。

今天我们要说的是__call() 和其他一些不常用的模式方法,我也许只会点到一下,如果你想搞的非常透彻还需要自己下功夫。

__call() 是个非常重要的魔术方法,可以说非常非常的重要。至于为什么,相信你学习完今天的笔记就会明白了。

当我们实例化一个类的对象后,调用类中的一个方法,比如$test->fun();。系统就会从这个类中去找fun()这个方法,如果找到了就去执行它,如果没有找到就去调用 __call()。
我们来看下例子:

<br />
					class Test {<br />
					<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __call($fun, $args) {&nbsp;&nbsp;//第一次参数是方法名,第二个参数是传过来的参数,是以数组的方式传过来的。<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;你在调用&quot;.$fun.&quot;方法&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; print_r($args);<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					$test-&gt;fun(&quot;a&quot;,&quot;b&quot;);&nbsp;&nbsp;//调用一个不存在的方法 参数是a和b .<br />
					//输出&nbsp;&nbsp;你在调用test方法Array ( [0] =&gt; a [1] =&gt; b )<br />
					

从上面的例子,我们可以清晰的分析出__call()方法的执行过程。但是我们想,如果类中存在一个同名的private方法,PHP会如何处理呢?我们来看下面的例子。

<br />
					class Test {<br />
					<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __call($fun, $args) {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;你在调用&quot;.$fun.&quot;方法&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; print_r($args);<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;private function fun($a, $b) {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;你再调用类中的private方法&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					$test-&gt;fun(&quot;a&quot;,&quot;b&quot;);&nbsp;&nbsp;//调用类中存在的private方法<br />
					//输出&nbsp;&nbsp;Fatal error: Call to private method Test::fun() from context &#39;&#39; in PHPDocument3 on line 14<br />
					

结果输出了错误,看来这种时候,魔术方法也不起作用。系统直接报错了。

现在我们再来想,如果调用本类中不存在的方法,而父类中存在的方法,是先调用本类的__call()呢还是先调用父类中存在的方法呢?我们继续来做实验。

<br />
					class Test {<br />
					<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __call($fun, $args) {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;你在调用父类的&quot;.$fun.&quot;方法&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; print_r($args);<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function fun() {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;你在调用父类的test方法&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					class Test2 extends Test {<br />
					<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __call($fun, $args) {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;你在调用子类的&quot;.$fun.&quot;方法&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; print_r($args);<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test2 = new Test2();<br />
					$test2-&gt;fun(&quot;a&quot;,&quot;b&quot;);&nbsp;&nbsp;//调用父类存在的方法&nbsp;&nbsp;输出&nbsp;&nbsp;你在调用父类的test方法<br />
					$test2-&gt;abc();&nbsp;&nbsp;//调用都不存在的方法&nbsp;&nbsp;输出&nbsp;&nbsp;你在调用子类的abc方法Array ( )<br />
					

结果,我们可到这样的现象,子类跳过了__call()而直接寻找父类中的方法,如果调用一个都不存在的方法,就调用本类的__call()方法,这个是由于子类的__call()把父类的__call()给覆盖了。

所以我们就可以得出这样的结论,在PHP中,系统对__call()方法的调用顺序是这样的:

当我们实例化一个类的对象后,调用类中的一个方法,比如$test->fun();。系统就会从这个类中去找fun()这个方法,如果存在就调用 之,如果不存在就去父类中找这个方法,如果父类中也不存在就去找本类的__call()方法,如果本类中不存在__call()方法就去找父类中的方法, 以此类推。当然这个是在调用权限允许的范围内的。

下面说我为什么说__call()重要。

我们都知道,在PHP5中并没有真正的支持重载。但是我们可以想办法去实现它,借助的工具就是__call()。我们来看例子。

<br />
					class Test {<br />
					<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __call($fun, $argps) {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if (method_exists($this, $fun.count($argps))) {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return call_user_func_array(array(&amp;$this, $fun.count($argps)), $argps);<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; } else {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;throw new Exception(&#39;调用了未知方法:&#39;.get_class($this).&#39;-&gt;&#39;.$fun);&nbsp;&nbsp;//不存在的方法<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					&nbsp; &nbsp; //一个参数的方法<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function fun1($a) {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;你在调用一个参数的方法,参数为:&quot;.$a;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					&nbsp; &nbsp; //两个参数的方法<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function fun2($a, $b) {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;你在调用两个参数的方法,参数为:&quot;.$a.&quot;和&quot;.$b;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					$test-&gt;fun(&quot;a&quot;);&nbsp;&nbsp;//输出&nbsp;&nbsp;你在调用一个参数的方法,参数为:a<br />
					$test-&gt;fun(&quot;a&quot;,&quot;b&quot;);&nbsp;&nbsp;//输出&nbsp;&nbsp;你在调用两个参数的方法,参数为:a和b<br />
					$test-&gt;fun(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;);&nbsp;&nbsp;//输出 Fatal error: Uncaught exception &#39;Exception&#39; with message &#39;调用了未知方法:Test-&gt;fun&#39;<br />
					

这样我们基本就可以完成了重载了。

另外提下下面不常用的两个魔术方法。
第一个是__toString() 。这个著名的方法在Java中是非常常见的,但是到PHP这里我基本没见多少人用过了。原因是这样的,我们看下例子:

<br />
					class Test {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $a;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function fun(){<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;我只是一个方法而已.&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					echo $test;&nbsp;&nbsp;//输出错误 Catchable fatal error: Object of class Test could not be converted to string<br />
					

如果我们想打印出一个对象,就需要调用__toString()这个魔术方法了,我们给他加上一个__toString()就不会出错了。

<br />
					class Test {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $a;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function fun(){<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;我只是一个方法而已.&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __toString() {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return &quot;你在打印一个对象&quot;;&nbsp;&nbsp;//这个方法必须返回一个字符串<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					echo $test;&nbsp;&nbsp;//输出&nbsp;&nbsp;你在打印一个对象<br />
					

这个比较简单,我不多说了,还有一个方法,在Google搜索都搜不到几个有价值的东西,可见其少用的程度,汗一下。这个魔术方法就是:__set_state();
这个方法其实也比较简单,就是var_export()的回调函数。我们来看下例子。

<br />
					class Test {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $a;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function fun(){<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; echo &quot;我只是一个方法而已.&quot;;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					var_export($test);&nbsp;&nbsp;//输出&nbsp;&nbsp;Test::__set_state(array( &#39;a&#39; =&gt; NULL, ))<br />
					

注意a是NULL,没有赋值,下面我写个回调

<br />
					class Test {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $a;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;static function __set_state($array) {&nbsp;&nbsp;//必须为静态方法.参数是个数组<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $tmp = new Test();<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $tmp-&gt;a = &quot;abc&quot;;&nbsp;&nbsp;//我直接赋值,<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return $tmp;&nbsp;&nbsp;//必须返回一个对象.可以是其他类的对象<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					eval( &#39;$b = &#39;&nbsp;&nbsp;. var_export($test, true).&nbsp;&nbsp;&#39;;&#39; );<br />
					var_dump($b);&nbsp;&nbsp;//得到的$b就是Test的一个对象.并且a=&quot;abc&quot;<br />
					

有人问这有什么用?是没多大用,最大的作用可以复制一个对象。只需改下代码而已。

<br />
					class Test {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $a;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;static function __set_state($array) {&nbsp;&nbsp;//必须为静态方法.参数是个数组<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $tmp = new Test();<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $tmp-&gt;a = $array[&#39;a&#39;];<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return $tmp;&nbsp;&nbsp;//必须返回一个对象.可以是其他类的对象<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					$test-&gt;a = &quot;我是$test&quot;;<br />
					eval( &#39;$b = &#39;&nbsp;&nbsp;. var_export($test, true).&nbsp;&nbsp;&#39;;&#39; );<br />
					var_dump($b);&nbsp;&nbsp;//得到的$b就是$test的复制.并且a=&quot;abc&quot;<br />
					

有人又会问了,克隆可以直接clone()方法啊!!那么好,这样的情况请问你如何克隆,我们来看下代码:

<br />
					class Test {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $a;<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;static function __set_state($array) {&nbsp;&nbsp;//必须为静态方法.参数是个数组<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $tmp = new Test();<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $tmp-&gt;a = str_replace(&#39;$test&#39;,&#39;$b&#39;,$array[&#39;a&#39;]);&nbsp;&nbsp;//!important<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return $tmp;&nbsp;&nbsp;//必须返回一个对象.可以是其他类的对象<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					$test-&gt;a = &#39;我是$test&#39;;<br />
					eval( &#39;$b = &#39;&nbsp;&nbsp;. var_export($test, true).&nbsp;&nbsp;&#39;;&#39; );<br />
					var_dump($b);&nbsp;&nbsp;//得到的$b就是$test的复制.但是b做相应的改变b=&#39;我是$b&#39;<br />
					

这样的话,我们虽然克隆了一个$test,但是我们又做了相应的改变。请允许我称为这种方法为“进化”。进化就是在克隆的基础上做一些自己相应的改变。
有的人问,你为什么不用__clone进行回调呢?这正是我想说的地方,个人认为__clone() 没有 __set_state()强大,因为__clone()没有可以接受的参数,局限了“进化”的范围,我用个例子说明。

<br />
					class Test {<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public $a;<br />
					<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;function __clone(){<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $this-&gt;a = str_replace(&quot;a&quot;,&quot;克隆a&quot;,$this-&gt;a);&nbsp;&nbsp;//无法通过参数改变a,但是我们还是可以想办法的:)<br />
					&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
					}<br />
					$test = new Test();<br />
					$test-&gt;a = &#39;我是a&#39;;<br />
					$b = clone $test;<br />
					var_dump($b);&nbsp;&nbsp;//得到的$b就是$test的复制.并且a=&#39;我是克隆a&#39;<br />
					

真累啊,基本方法都提到了,但是我只是带大家入门,更多更深的内容还需要大家自己挖掘。有些地方我还没有提到,所以大家要学会去主动学习。

另外,顺便提下,这些魔术方法都必须被定义为public方式的。因为你要在所有的地方都得调用它们。

好了。这个还没完,下此会有更神奇的东西,改变你对PHP的看法,请关注。

<br />
						<br />
						<br />
						&lt;?php<br />
						<br />
						<br />
						class Test {<br />
						&nbsp; &nbsp; private $mySecret;&nbsp;&nbsp;//我的秘密不想让人知道<br />
						&nbsp; &nbsp; public function __construct($secret) {<br />
						&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$this-&gt;mySecret = $secret;<br />
						&nbsp; &nbsp; }<br />
						&nbsp; &nbsp; public function __sleep() {<br />
						&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$this-&gt;mySecret = &quot;这是个秘密!&quot;;&nbsp; &nbsp; // 外部不可见.必须调用成员方法输出值.<br />
						&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;echo &#39;不让你知道&#39;;&nbsp;&nbsp;<br />
						<br />
						<br />
						&nbsp; &nbsp; }<br />
						&nbsp; &nbsp; public function __wakeup() {<br />
						&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br />
						&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;<br />
						&nbsp; &nbsp;&nbsp; &nbsp;<br />
						<br />
						&nbsp; &nbsp; }<br />
						&nbsp; &nbsp; &nbsp; &nbsp; <br />
						&nbsp; &nbsp; &nbsp; &nbsp; public function get(){<br />
						&nbsp; &nbsp; &nbsp; &nbsp; <br />
						&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;mySecret;<br />
						&nbsp; &nbsp; &nbsp; &nbsp; }<br />
						}<br />
						<br />
						<br />
						<br />
						$obj = new Test(&#39;i like php !&#39;);<br />
						echo $obj-&gt;get();<br />
						echo &#39;&lt;br /&gt;&#39;;<br />
						<br />
						serialize($obj);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // __sleep()<br />
						echo $obj-&gt;get();<br />
						echo &#39;&lt;br /&gt;&#39;;<br />
						<br />
						unserialize($obj);&nbsp; &nbsp;// __wakeup()<br />
						echo $obj-&gt;get();<br />
						<br />
						<br />
						$obj = new Test(&#39;php&#39;);<br />
						echo $obj-&gt;get();<br />
						<br />
						<br />
						<br />
						?&gt;<br />
						<br />
						<br />
						
大家等我教程等着急了吧?这几天事情比较多。

看到很多人看完上面的笔记还是有很多人不明白,不清楚,或者说有疑问的地发。不要着急,我今天绝对打消您的疑问。您瞧好吧。

今天我们利用前几天学习的知识,学习如何写一个动态类,其实是就是一个具有柔韧性的类。

什么是一个具有柔韧性的类?其实我也说不清楚,有的人更愿意称之为可以弯曲的类,因为这个类可以随你的意愿进行有特点的伸展。

我们上次提到了__set()这个魔术方法,又提到了,其实魔术方法之所以称之为魔术方法是由于我们不用主动去触发这个方法它就会自动的运行。

有的人会问了,在学习__set()的时候,我们是主动去调用它的啊。跟别的,比如__isset()方法有些不一样。其实在真正的运用中,__set()的使用方法不是上面那样的,请允许我为这篇文章埋下一个伏笔。今天我们就来学习__set()的最神奇的地方。

一个柔韧性的类的一个重要的特征应该就是属性具有柔韧度。如何制作一个具有伸缩属性的类?看下面的例子。

<br />
					class Test {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; private $array;&nbsp;&nbsp;//PHP数组就是具有伸缩性的这个特性<br />
					<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function __set($key, $value) {&nbsp;&nbsp;//__set()魔术方法,设置$array的值。<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;array[$key] = $value;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function get() {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($this-&gt;array);<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					}<br />
					$test = new Test();<br />
					$test-&gt;a = &#39;a&#39;;&nbsp;&nbsp;//这样系统就会自动调用__set()这个魔术方法<br />
					$test-&gt;b = &#39;b&#39;;&nbsp;&nbsp;//这样下去,我们就可以设置无数个属性。<br />
					$test-&gt;c = &#39;c&#39;;&nbsp;&nbsp;//你还可以设置$test-&gt;d 。没有人拦着你,谁拦你我拦他!<br />
					$test-&gt;get();&nbsp;&nbsp;//输出&nbsp;&nbsp;Array ( [a] =&gt; a <b> =&gt; b  =&gt; c )<br />
					

这只是个简单的例子,如果你要限制一些属性,该如何设置呢?还记得前面的课程吗?

<br />
					class Test {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; private $array;<br />
					<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function __construct() {&nbsp;&nbsp;//在构造函数中我们可以设置一些属性。<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;array[&#39;a&#39;] = null;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;array[&#39;b&#39;] = null;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function __set($key, $value) {&nbsp;&nbsp;//__set()魔术方法,进行一下简单的判断就OK<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array_key_exists($key, $this-&gt;array)) {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;array[$key] = $value;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception(&quot;没有此属性&quot;);<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function get() {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($this-&gt;array);<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					}<br />
					$test = new Test();<br />
					$test-&gt;a = &#39;a&#39;;<br />
					$test-&gt;b = &#39;b&#39;;<br />
					$test-&gt;c = &#39;c&#39;;&nbsp;&nbsp;//当程序执行到这步的时候就会抛出异常。<br />
					$test-&gt;get();<br />
					

有人肯定又会说了,我靠。你耍我们玩啊,一会说要写个可以伸缩的类,然后又说要做限制,你怎么回事啊?

莫急莫急,我这就跟您慢慢解释。要写个具有伸缩性的类,上面我说了最简单的例子了,大家应该都明白了。

但是在实际的程序中,我们并不需要这样伸缩性的类,因为这样的类会变得不安全,未知因素也很多。这不是我们设计程序的初衷。

但是,在实际的运用中,我们又需要根据实际情况来设定一些变化的因素。这样就会有了矛盾了,既要伸缩性,但是在某种程度上又不允许程序具有伸缩性。

这是个非常棘手的问题,虽然我们绕弯避免。最简单的解决办法就是多申请几个属性,想用就用。但是我们既然学了魔术方法,就要使用魔术方法来做点事情。值得 注意的是,构造函数在PHP里也算是一个魔术方法。所以我们来写个要伸就伸,要缩就缩的类来。这种类的用处非常大,等下你就知道了。

<br />
					class TestA {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; private $array = array();<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function __construct($array) {&nbsp;&nbsp;//在父类中创建一个具有伸缩性的构造方法<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( $array as $key )<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;array[ $key ] = null;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function __set($key, $value) {&nbsp;&nbsp;//__set()魔术方法,进行一下简单的判断就OK<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array_key_exists($key, $this-&gt;array)) {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;array[$key] = $value;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception(&quot;没有此属性&quot;);<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function get() {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($this-&gt;array);<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					}<br />
					class TestB extends TestA {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function __construct($array) {&nbsp;&nbsp;//只要重写构造函数就可以申请自己的属性值.<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct($array);&nbsp;&nbsp;//调用父类的构造方法<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					}<br />
					$test1 = new TestB(array(&#39;a&#39;, &#39;b&#39;));&nbsp;&nbsp;//想要几个属性就要几个属性<br />
					$test1-&gt;a = &#39;a&#39;;<br />
					$test1-&gt;b = &#39;b&#39;;<br />
					$test1-&gt;get();&nbsp;&nbsp;//输出&nbsp;&nbsp;Array ( [a] =&gt; a <b> =&gt; b )<br />
					$test2 = new TestB(array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;));&nbsp;&nbsp;//想要几个属性就要几个属性<br />
					$test2-&gt;a = &#39;a&#39;;<br />
					$test2-&gt;b = &#39;b&#39;;<br />
					$test2-&gt;c = &#39;c&#39;;<br />
					$test2-&gt;get();&nbsp;&nbsp;//输出&nbsp;&nbsp;Array ( [a] =&gt; a <b> =&gt; b  =&gt; c )<br />
					

这下明白了吧?我们是通过继承来实现这种中性的伸缩的。这就叫伸缩自如。哈哈。

如果你体会到了其中的好处,你会发现,PHP的类在这方面会比Java要强大的多。我们下面试图用这种思路来写一个简单的数据库类。

<br />
					class DB {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function connect($dbhost, $dbuser, $dbpwd, $dbname = &#39;&#39;, $charset = &#39;utf8&#39;) {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql_connect ( $dbhost, $dbuser, $dbpwd );<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql_query ( &quot;SET NAMES &#39;$charset&#39;&quot; );<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql_select_db ( $dbname );<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function insert($sql) {&nbsp; &nbsp;//插入数据<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (! ($query = mysql_query ( $sql ))) {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mysql_insert_id();<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					}<br />
					

上面这是个最基础的类,我写的很垃圾,你可以再修改,但是我们现在最重要的是让他可以具有伸缩性。所以我这样修改。

<br />
					class DB {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; private $table = &#39;&#39;;&nbsp;&nbsp;//定义表<br />
					&nbsp; &nbsp; &nbsp; &nbsp; private $field = array();&nbsp;&nbsp;//表里面的字段<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function __construct($table, $field) {&nbsp;&nbsp;//在父类中创建一个具有伸缩性的构造方法<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;table = $table;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( $field as $key ) {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;$field[ $key ] = null;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function connect($dbhost, $dbuser, $dbpwd, $dbname = &#39;&#39;, $charset = &#39;utf8&#39;) {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql_connect ( $dbhost, $dbuser, $dbpwd );<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql_query ( &quot;SET NAMES &#39;$charset&#39;&quot; );<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql_select_db ( $dbname );<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function insert() {&nbsp;&nbsp;//插入一个数据<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fieldName = join(&quot;, &quot;, array_keys($this-&gt;field));&nbsp;&nbsp;//获得字段名<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_walk($this-&gt;field, array(&amp;$this, &#39;filter&#39;));&nbsp;&nbsp;//过滤字段的值.可以看下下面的回调函数<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fieldValue = join(&quot;, &quot;, $this-&gt;field);&nbsp;&nbsp;//转换成**,**,**这种形式<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql = &quot;INSERT INTO %s(%s) VALUES(%s)&quot;;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql = sprintf($sql, $this-&gt;table, $fieldName, $fieldValue);<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!(mysql_query ( $sql ))) {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return mysql_insert_id();<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function filter(&amp;$value) {&nbsp;&nbsp;//过滤字段的值<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!is_numeric($value)) {&nbsp; &nbsp; //如果是数字就不管.如果是其他类型必须加入&#39;&#39;才可以正常插入到数据库中<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $value =&nbsp;&nbsp;&quot;&#39;&quot;.addslashes($value).&quot;&#39;&quot;;<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					}<br />
					

你看完后,肯定会说,好麻烦啊,你为什么把一个简单的插入数据搞得这么复杂啊。你傻吧你!

很客观的告诉你,我不傻,这么做是有好处的。如果我写了一个这样类,那么面向用户是非常好的,扩展性非常好,并且容易操作。我们来看下,当你把我这个类当做父类后,看下你的操作是多么的简单。

<br />
					class TableA extends DB {<br />
					&nbsp; &nbsp; &nbsp; &nbsp; public function __construct() {&nbsp;&nbsp;//只要重写构造函数就可以申请自己的属性值.<br />
					&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct(&#39;tablea&#39;,array(&#39;id&#39;, &#39;key&#39;, &#39;value&#39;));&nbsp;&nbsp;//调用父类的构造方法<br />
					&nbsp; &nbsp; &nbsp; &nbsp; }<br />
					}<br />
					$tablea = new TableA();<br />
					$tablea-&gt;id = 123;<br />
					$tablea-&gt;key = &#39;我是key&#39;;<br />
					$tablea-&gt;value = &#39;我是value&#39;;<br />
					$tablea-&gt;insert();<br />
					

看到了吧?我们几乎可以使MySqL变成一个对象型的数据库,因为我们可以给每个表都写个类,一个类对应一个表,执行操作的时候不用写SQL语句,非常简单,不容易出错,这不是传说中大名鼎鼎的Hibernate吗?

说Hibernate有点言过其实了,主要是我们改变了PHP与MySql的一般操作方式。但是这样的问题就在于父类不好写,比较麻烦。对于一般的新手还是非常困难的,所以希望个大牛能写出个通用的父类来,感激不尽啊。

好了,拖了这么长时间,笔记终于算写完了,但是PHP中魔术方法的内容远远没有这么少,所以要学习还得靠自己。我以后还会不断的出类似这样的教程笔记,希望大家关注,谢谢,再次感谢您能看完我这么多字的胡扯。


觉得set之所以称为魔术方法应该是这个原因
<?php
class test
{
        public $a;
        public function __set($name,$value)
        {
                $this->a[$name]=$value;
        }
}
$a=new test();
$a->b=2;
echo $a->a['b'];


<br />
			<br />
			&lt;?php<br />
			class Test {<br />
			&nbsp; &nbsp; private $a;<br />
			<br />
			&nbsp; &nbsp; public function __set($name, $value) {&nbsp;&nbsp;//必须是两个参数<br />
			<br />
			<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$this-&gt;$name= $value;<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//这里不明白为什么是$this-&gt;$name ?? 一般不是这样写吗? <br />
			<br />
			<br />
			&nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; public function __get($name) {&nbsp;&nbsp;//一个参数,要获得的属性<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if( isset($this-&gt;$name))<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;echo &quot;{$name} 的值是:&quot;;&nbsp; &nbsp; &nbsp; &nbsp; <br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return $this-&gt;$name;&nbsp; &nbsp; &nbsp; &nbsp; <br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
			&nbsp; &nbsp; &nbsp; &nbsp; <br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;$this-&gt;$name=0;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;return &quot;{$name} 未设置, {$name} 被初始化为0.&lt;br /&gt;&quot;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; }<br />
			}<br />
			$test = new Test();<br />
			$test-&gt;__set(&#39;a&#39;, &#39;我是A&#39;);&nbsp;&nbsp;<br />
			echo $test-&gt;__get(&#39;a&#39;);<br />
			echo &#39;&lt;br /&gt;&#39;;<br />
			echo $test-&gt;__get(&#39;b&#39;);<br />
			echo $test-&gt;__get(&#39;b&#39;);<br />
			?&gt;`<br />
			<br />
			<br />
			


<br />
			<br />
			<br />
			&lt;?php<br />
			<br />
			<br />
			class Test {<br />
			&nbsp; &nbsp; //var $a;&nbsp;&nbsp;//这样就不输出<br />
			&nbsp; &nbsp;// public $a;&nbsp;&nbsp;//这样也不输出<br />
			<br />
			&nbsp; &nbsp; //protected $a;&nbsp;&nbsp;//输出 你小子偷看我??<br />
			&nbsp; &nbsp; private $a;&nbsp;&nbsp;//输出 你小子偷看我??<br />
			<br />
			&nbsp; &nbsp; public function __construct($value) {<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$this-&gt;a = $value;<br />
			&nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; public function __isset($name) {<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;echo &quot;你小子偷看我??&quot;;<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return ($name);&nbsp;&nbsp;//返回真是的信息<br />
			&nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; public function __unset($name) {<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;echo &quot;你没权利宰了我!&quot;;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($name);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;// 将name 改为$this-&gt;a&nbsp;&nbsp;才消除a??&nbsp;&nbsp;<br />
			&nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; &nbsp; &nbsp; public function get(){<br />
			&nbsp; &nbsp; &nbsp; &nbsp; <br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-&gt;a;<br />
			&nbsp; &nbsp; &nbsp; &nbsp; <br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			<br />
			<br />
			<br />
			<br />
			}<br />
			$test = new Test(&quot;我是a&quot;);<br />
			isset($test-&gt;a);<br />
			unset($test-&gt;a);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// isset 和 __isset() 之间的执行关系是怎样的????<br />
			echo $test-&gt;get();<br />
			<br />
			?&gt;<br />
			<br />
			<br />
			


<br />
			&lt;?php<br />
			<br />
			<br />
			class Test {<br />
			&nbsp; &nbsp; //var $a;&nbsp;&nbsp;//这样就不输出<br />
			&nbsp; &nbsp;// public $a;&nbsp;&nbsp;//这样也不输出<br />
			<br />
			&nbsp; &nbsp; //protected $a;&nbsp;&nbsp;//输出 你小子偷看我??<br />
			&nbsp; &nbsp; private $a;&nbsp;&nbsp;//输出 你小子偷看我??<br />
			<br />
			&nbsp; &nbsp; public function __construct($value) {<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$this-&gt;a = $value;<br />
			&nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; public function __isset($name) {<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;echo &quot;你小子偷看我??&quot;;<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return isset($name);&nbsp;&nbsp;//&nbsp;&nbsp;这里需要返回一个bool类型.应该这样写&nbsp;&nbsp;如果不想让别人知道就直接 return false;<br />
			&nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; public function __unset($name) {<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;echo &quot;你没权利宰了我!&quot;;<br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;unset($name);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;// $name为形参.当然不能删除$this-&gt;a<br />
			&nbsp; &nbsp; }<br />
			<br />
			&nbsp; &nbsp; public function get(){<br />
			&nbsp; &nbsp; <br />
			&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return $this-&gt;a;<br />
			&nbsp; &nbsp; <br />
			&nbsp; &nbsp; }<br />
			<br />
			<br />
			<br />
			<br />
			}<br />
			$test = new Test(&quot;我是a&quot;);<br />
			isset($test-&gt;a);<br />
			unset($test-&gt;a);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// 当执行isset()的时候.系统就去找是否存在__isset() 如果存在就去执行.就是isset()的一个回调.<br />
			echo $test-&gt;get();<br />
			<br />
			?&gt;<br />
			<br />
			

邵珠庆推荐文章

博文加载中...

anyShare分享到:

喜欢这个文章吗?

考虑订阅我们的RSS Feed吧!

发布在 邵珠庆

评论 (3) 引用 (0)
  1. Obama and his Attorney General Eric Holder have publicly stated that some prisoners in Guantanamo will never be released even if proven guilty at trial since they are too dangerous. 

  2. I hope the line up for reading fest is this good or better next year! Soo excited even though I have to wait 1 1/2 years asdfghjkl

  3. Star Wars The Old Republic Release…


Leave a comment

*

还没有引用.