<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]PHP5学习笔记之PHP魔法函数：__call()  ，以及用__call()实现方法重载 。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Wed, 17 Mar 2010 11:22:53 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	_call()函数是php类的默认魔法函数，__call() 在一个对象的上下文中，如果调用的方法不能访问，它将被触发,可以用它来做重载，如果一个类的方法不存在，则需要重新加载一次。<br/><br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
class MethodTest &#123;
&nbsp;&nbsp;&nbsp;&nbsp; public function __call($name, $arguments) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Note: value of $name is case sensitive.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;Calling object method &#039;$name&#039; &quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . implode(&#039;, &#039;, $arguments). &quot;&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp; &#125;
&#125;
$obj = new MethodTest;
$obj-&gt;runTest(&#039;in object context&#039;);
</textarea><br/><br/>运行结果：<br/>Calling object method &#039;runTest&#039; in object context<br/>以上来自：<br/>http://blog.163.com/lgh_2002/blog/static/4401752620105256371802/<br/><br/>用__call()实现方法重载 ：<br/><div class="code"><br/>&lt;?php<br/>class foo &#123;<br/>&nbsp;&nbsp;function __call($name,$arguments) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Did you call me? I&#039;m $name!&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;print_r($arguments);<br/>&nbsp;&nbsp;&#125;<br/>&#125; $x = new foo();<br/>$array = array(1,2,2,1984);<br/>$x-&gt;doStuff(&quot;dfdf&quot;);<br/>$x-&gt;fancy_stuff($array,&quot;777&quot;);<br/>?&gt;<br/></div><br/><br/>Did you call me? I&#039;m doStuff!Array ( [0] =&gt; dfdf ) Did you call me? I&#039;m fancy_stuff!Array ( [0] =&gt; Array ( [0] =&gt; 1 [1] =&gt; 2 [2] =&gt; 2 [3] =&gt; 1984 ) [1] =&gt; 777 )<br/><br/>$arguments 0,1,2,3 分别对应如果没有该函数的三个函数参数。<br/>$name表示缺失的函数名称！<br/><br/>使用 __call 实现“过载”动作<br/><div class="code">&lt;?php<br/>class Magic &#123;<br/>&nbsp;&nbsp;function __call($name,$arguments) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if($name==&#039;foo&#039;) &#123;<br/>&nbsp;&nbsp;if(is_int($arguments&#91;0&#93;)) $this-&gt;foo_for_int($arguments&#91;0&#93;);<br/>&nbsp;&nbsp;if(is_string($arguments&#91;0&#93;)) $this-&gt;foo_for_string($arguments&#91;0&#93;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&#125;&nbsp;&nbsp; private function foo_for_int($x) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;oh an int!&quot;);<br/>&nbsp;&nbsp;&#125;&nbsp;&nbsp; private function foo_for_string($x) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;oh a string!&quot;);<br/>&nbsp;&nbsp;&#125;<br/>&#125; $x = new Magic();<br/>$x-&gt;foo(3);<br/>$x-&gt;foo(&quot;3&quot;);<br/>?&gt;</div><br/><br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
class abc&#123;
&nbsp;&nbsp;&nbsp;&nbsp;public function __call($method, $arguments=NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($method == &quot;not_exist_fun&quot;)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;not_exist_fun is called but not exist,U can do it by yourselft.&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;方法不存在&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;public function abcef()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;456&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;
$a = new abc();
echo ($a-&gt;abcef());
echo ($a-&gt;not_exist_fun());
echo ($a-&gt;not_exist_fun_2());
?&gt;
</textarea><br/>---------- 调试PHP情况 ----------<br/>456<br/>not_exist_fun is called but not exist,U can do it by yourselft.<br/>方法不存在<br/><br/>————————————————————————————————————————————————————————<br/>如果在框架里用它，是这样的，PHP5里有一个反射函数，进行autoload进相关文件，后对该类的Obj进行：Obj-&gt;fun();<br/>如果这个fun不存在就调用这个__call，这个call是上层类，其autoload时new的是这个继承的类名，于是有，如下伪代码：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
class OpenApi_BaseController &#123;
&nbsp;&nbsp;function __call($name,$arguments) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;__call Function called:Did you call me? I&#039;m $name!&#92;n&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;print_r($arguments);
&nbsp;&nbsp;&#125;
&#125;
class OpenApi_MobileController extends OpenApi_BaseController&#123;
&nbsp;&nbsp;function getMobileNumber($name,$arguments) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Did you call me? I&#039;m $name!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;print_r($arguments);
&nbsp;&nbsp;&#125;
&#125;
$x = new OpenApi_MobileController();
$x-&gt;doStuff(&quot;dfdf&quot;,array(&quot;jack&quot;,&quot;xiang&quot;));
?&gt;
</textarea>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]PHP5学习笔记之PHP魔法函数：__call()  ，以及用__call()实现方法重载 。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>