精英盒子 -> 程序设计 -> PHP的函数重载(伪重载) [打印本页]

abreto 2011-08-24 10:31

PHP的函数重载(伪重载)

字数有限,只贴代码,详见原文。
  1. <?php
  2. /** 零个参数 */
  3. function hello0()
  4. {
  5.     echo 'hello0<br />';
  6. }
  7. /** 一个参数 */
  8. function hello1($param)
  9. {
  10.     echo 'hello1'.$param.'<br />';
  11. }
  12. /** 两个参数 */
  13. function hello2($pa1, $pa2)
  14. {
  15.     echo 'hello2'.$pa1.$pa2.'<br />';
  16. }
  17. /** 三个参数 */
  18. function hello3($p1, $p2, $p3)
  19. {
  20.     echo "hello3{$p1}{$p2}{$p3}<br />";
  21. }
  22. /** 不解释 */
  23. function hello($_=NULL)        // 个人以为$_=NULL就相当于表示参数不定,不赞同的无视它吧
  24. {
  25.     $num = func_num_args();
  26.   
  27.     /**
  28.      * __FUNCTION__取得当前函数名,即'hello',$num是当前的参数个数
  29.      * __FUNCTION__.$num 即实际调用的函数
  30.      * 关于call_user_func_array大家可以去网上查阅资料,在此不再赘述
  31.      *
  32.      */
  33.     call_user_func_array(__FUNCTION__.$num, func_get_args());
  34. }
  35. // 测试一下---------------------
  36. hello();
  37. hello('param1');
  38. hello('p1', '|', 'p3');
  39. hello("two", '2nd');
  40. ?>

  1. <?php
  2. /**
  3. * 参考:
  4. * 整数:integer, 浮点数:double, 布尔:boolean, 字符串:string, 资源:resourrce
  5. * 数组:array, 对象:object, 未知类型:unknown type;
  6. */
  7. /** 木有参数 */
  8. function hello_void()
  9. {
  10.     echo 'hello without param<br />';
  11. }
  12. /** 一个整数参数 */
  13. function hello_integer($n)
  14. {
  15.     echo $n.'+1='.($n+1).'<br />';
  16. }
  17. /** 两个整数参数 */
  18. function hello_integer_integer($num0, $num1)
  19. {
  20.     echo ($num0+$num1).'<br />';
  21. }
  22. /** 两个字符串参数 */
  23. function hello_string_string($str0, $str1)
  24. {
  25.     echo "{$str0}{$str1}<br />";
  26. }
  27. /** 两个数组参数,一个布尔 */
  28. function hello_array_array_boolean($arr0, $arr1, $b)
  29. {
  30.     if($b)
  31.         print_r($arr0);
  32.     print_r($arr1);
  33.     echo '<br />';
  34. }
  35. /** 不解释。 */
  36. function hello($_=NULL)
  37. {
  38.     // 此函数根据参数的个数和类型动态的选择执行哪个函数
  39.     $__ = '';    // 这个字符串用于添加到函数名的末尾组成真正调用的函数名
  40.     if(!func_num_args())    // 如果没有参数
  41.         $__ = '_void';        // 置$__为_void,则__FUNCTION__.$__就是'hello_void'
  42.     else    // 如果有参数
  43.         foreach(func_get_args() as $arg)    // func_get_args()返回当前参数的一个数组,按顺序遍历之
  44.             $__ .= '_'.gettype($arg);        // 根据当前参数的类型在$__后加上 _<参数类型>
  45.     // 调用__FUNCTION__.$__,以func_get_args()为参数
  46.     // 最后 $__ 实际上就是 _<参数一类型>_<参数二类型>.._<参数n类型>,
  47.     // __FUNCTION__当前函数名即hello, __FUNCTION__.$__就是实际调用的函数
  48.     call_user_func_array( __FUNCTION__.$__ , func_get_args() );
  49. }
  50. hello();
  51. hello(1);
  52. hello(1, 2);
  53. hello('str0', 'str1');
  54. hello(array(0, 1), array(2, 3), false);
  55. ?>



http://blog.abreto.net/blog/2011/08/php%E7%9A%84%E5%87%BD%E6%95%B0%E9%87%8D%E8%BD%BD%E4%BC%AA%E9%87%8D%E8%BD%BD.html
原文地址:

outman 2011-08-24 10:47
-、- 路过

jybox 2011-08-24 14:57
好吧  我不得不说写的很强大   好创意啊
创意是不过,不过估计没人闲的蛋疼来用这种方式来重载php函数

abreto 2011-08-24 18:14
C++的内部其实就是这种方式,你仔细看他生成的符号名称

abreto 2011-08-25 16:46
其实你是赤果果的嫉妒,哈哈

jybox 2011-08-25 17:31
这是事实,C++的重载当然有人用
但是试问有多少人用这种方法重载PHP函数?1%不到吧?

abreto 2012-06-04 20:55
为什么抠十分?

abreto 2012-06-04 20:55
jybox:[表情] 这是事实,C++的重载当然有人用
但是试问有多少人用这种方法重载PHP函数?1%不到吧? (2011-08-25 17:31) 

可以简化一下........

jybox 2012-06-04 21:01
abreto:可以简化一下........ (2012-06-04 20:55) 

你得把细节都封装好,让用户只需要像C++那么重载,才有可用性

abreto 2012-06-04 22:12
jybox:你得把细节都封装好,让用户只需要像C++那么重载,才有可用性 (2012-06-04 21:01) 

是的,是的,是这样的,我是这么想的




Powered by phpwind v8.7 Code ©2003-2011 phpwind
Time 0.058762 second(s),query:5 Gzip enabled