<?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[两年前写的php之call_user_func_array的简易用法]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Fri, 12 Mar 2010 10:01:48 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	今天在群里面，有个叫lewis的在问call_user_func_array的用法，因为之前一直没有用过，也不能说什么，于是看一下手册，发现是这么写的：<br/>call_user_func_array<br/><br/>(PHP 4 >= 4.0.4, PHP 5)<br/>call_user_func_array --&nbsp;&nbsp;Call a user function given with an array of parameters<br/>Description<br/>mixed call_user_func_array ( callback function, array param_arr )<br/><br/>Call a user defined function given by function, with the parameters in param_arr.<br/>然后还有一个例子：<br/><br/>PHP代码<br/><br/><div class="code"><br/>&nbsp;&nbsp; 1. &lt;?php&nbsp;&nbsp;<br/>&nbsp;&nbsp; 2. function debug($var, $val)&nbsp;&nbsp; <br/>&nbsp;&nbsp; 3. &#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp; 4.&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;***DEBUGGING&#92;nVARIABLE: $var&#92;nVALUE:&quot;;&nbsp;&nbsp;<br/>&nbsp;&nbsp; 5.&nbsp;&nbsp;&nbsp;&nbsp; if (is_array($val) &#124;&#124; is_object($val) &#124;&#124; is_resource($val)) &#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp; 6.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print_r($val);&nbsp;&nbsp;<br/>&nbsp;&nbsp; 7.&nbsp;&nbsp;&nbsp;&nbsp; &#125; else &#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp; 8.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;&#92;n$val&#92;n&quot;;&nbsp;&nbsp;<br/>&nbsp;&nbsp; 9.&nbsp;&nbsp;&nbsp;&nbsp; &#125;&nbsp;&nbsp;<br/>&nbsp;&nbsp;10.&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;***&#92;n&quot;;&nbsp;&nbsp;<br/>&nbsp;&nbsp;11. &#125;&nbsp;&nbsp;<br/>&nbsp;&nbsp;12.&nbsp;&nbsp; <br/>&nbsp;&nbsp;13. $c = mysql_connect();&nbsp;&nbsp;<br/>&nbsp;&nbsp;14. $host = $_SERVER&#91;&quot;SERVER_NAME&quot;&#93;;&nbsp;&nbsp;<br/>&nbsp;&nbsp;15.&nbsp;&nbsp; <br/>&nbsp;&nbsp;16. call_user_func_array(&#039;debug&#039;, array(&quot;host&quot;, $host));&nbsp;&nbsp;<br/>&nbsp;&nbsp;17. call_user_func_array(&#039;debug&#039;, array(&quot;c&quot;, $c));&nbsp;&nbsp;<br/>&nbsp;&nbsp;18. call_user_func_array(&#039;debug&#039;, array(&quot;_POST&quot;, $_POST));&nbsp;&nbsp;<br/>&nbsp;&nbsp;19. ?&gt;&nbsp;&nbsp; </div><br/><br/>相信看了例子之后应该有点明白了吧？<br/>我自己是这么理解这个函数的，如果说的不对，还望各位高手不要耻笑：<br/>&nbsp;&nbsp;&nbsp;&nbsp; 该函数真正的用法有点类似于函数重载，因为他的第一个参数是字符型的，也就是函数的名称，第二个参数是数组，我们可以当成该函数的各个参数，而事实上也就是这么用的，如果你看过我的前一篇文章：PHP的伪重载 ，或许你能够理解，正是因为这个函数的存在，我发现函数重载也可以这样运用：<br/><br/>PHP代码<br/><br/><br/><div class="code">&nbsp;&nbsp; 1. &lt;?php&nbsp;&nbsp;<br/>&nbsp;&nbsp; 2. /** <br/>&nbsp;&nbsp; 3. * 例子写完后，本来认为完事了，结果遇到有人问call_user_func_array(),看了一下手册 <br/>&nbsp;&nbsp; 4. * 原来，我上面的那个test函数还可以精简成如下的例子， <br/>&nbsp;&nbsp; 5. */&nbsp;&nbsp;<br/>&nbsp;&nbsp; 6. function otest1 ($a)&nbsp;&nbsp;<br/>&nbsp;&nbsp; 7. &#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp; 8.&nbsp;&nbsp;&nbsp;&nbsp; echo( &#039;一个参数&#039; );&nbsp;&nbsp;<br/>&nbsp;&nbsp; 9. &#125;&nbsp;&nbsp;<br/>&nbsp;&nbsp;10.&nbsp;&nbsp; <br/>&nbsp;&nbsp;11. function otest2 ( $a, $b)&nbsp;&nbsp;<br/>&nbsp;&nbsp;12. &#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp;13.&nbsp;&nbsp;&nbsp;&nbsp; echo( &#039;二个参数&#039; );&nbsp;&nbsp;<br/>&nbsp;&nbsp;14. &#125;&nbsp;&nbsp;<br/>&nbsp;&nbsp;15.&nbsp;&nbsp; <br/>&nbsp;&nbsp;16. function otest3 ( $a ,$b,$c)&nbsp;&nbsp;<br/>&nbsp;&nbsp;17. &#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp;18.&nbsp;&nbsp;&nbsp;&nbsp; echo( &#039;三个啦&#039; );&nbsp;&nbsp;<br/>&nbsp;&nbsp;19. &#125;&nbsp;&nbsp;<br/>&nbsp;&nbsp;20.&nbsp;&nbsp; <br/>&nbsp;&nbsp;21. function otest ()&nbsp;&nbsp;<br/>&nbsp;&nbsp;22. &#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp;23.&nbsp;&nbsp;&nbsp;&nbsp; $args = func_get_args();&nbsp;&nbsp;<br/>&nbsp;&nbsp;24.&nbsp;&nbsp;&nbsp;&nbsp; $num = func_num_args();&nbsp;&nbsp;<br/>&nbsp;&nbsp;25.&nbsp;&nbsp;&nbsp;&nbsp; call_user_func_array( &#039;otest&#039;.$num, $args&nbsp;&nbsp;);&nbsp;&nbsp;<br/>&nbsp;&nbsp;26. &#125;&nbsp;&nbsp;<br/>&nbsp;&nbsp;27.&nbsp;&nbsp; <br/>&nbsp;&nbsp;28. otest(1,2);&nbsp;&nbsp;</div><br/><br/>看到不？而我最初的写法，在PHP的伪重载一文中有所提及，仅作参考。。。。<br/><br/>这些只是call_user_func_array的简易用法，在PHP4下测试过，而手册中还有一些将第一个参数当成数组来传入的例子，我在PHP4下是没有办法运行的，也许PHP5可以吧，但我不用PHP5的，也没有办法解释什么。谢谢各位 以前一直用PHP4的，现在用PHP5了，关于这个函数，大家可以看看thinkphp的functions.php中的getInstance方法，也是一个很好的诠释哦<br/><br/>膘哥的blog：<br/>http://www.neatstudio.com/show-300-1.shtml<br/>http://www.joomlagate.com/component/option,com_smf/Itemid,31/topic,7594.0/
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 两年前写的php之call_user_func_array的简易用法]]></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>