PHP的伪重载
时间:2011-01-29 来源:枫叶
     <? php
2
3
4 // 今天在看书的时候,发现书上有这么一条:函数重载的替代方法————伪重载
 // 今天在看书的时候,发现书上有这么一条:函数重载的替代方法————伪重载
5 //
//
6 //确实,在PHP中没有函数重载这个概念,让很多时候我们无法进行一些处理,甚至有时候不得不在函数后面定义好N个参数
//确实,在PHP中没有函数重载这个概念,让很多时候我们无法进行一些处理,甚至有时候不得不在函数后面定义好N个参数
7 //在看到了func_get_arg,func_get_args,func_num_args,这三个函数的时候,你们是不是想起了什么?
//在看到了func_get_arg,func_get_args,func_num_args,这三个函数的时候,你们是不是想起了什么?
8 //SO,let's try it
//SO,let's try it ..
..
9
10 function  testOne (  $a  )
  function  testOne (  $a  )
11 {
 {
12 echo (  ' 一个参数就这样 '  );
   echo (  ' 一个参数就这样 '  );
13 }
 }
14
15 function  testTwo (  $a   ,   $b )
  function  testTwo (  $a   ,   $b )
16 {
 {
17 echo (  ' 两个参数的就这样 '  );
   echo (  ' 两个参数的就这样 '  );
18 }
 }
19
20 function  testThree (  $a   ,   $b   ,   $c )
  function  testThree (  $a   ,   $b   ,   $c )
21 {
 {
22 echo (  ' 黑黑,这是三个参数的 '  );
   echo (  ' 黑黑,这是三个参数的 '  );
23 }
 }
24
25 function  test ()
  function  test ()
26 {
 {
27 $argNum   =   func_num_args ();
   $argNum   =   func_num_args ();
28 // 这一段其实可以用 $_arg = func_get_args() 来获得所有的参数,只是要用数组而已,不方便我下面的表达,呵呵
   // 这一段其实可以用 $_arg = func_get_args() 来获得所有的参数,只是要用数组而已,不方便我下面的表达,呵呵
29 for  (  $i   =   0 ;  $i   <   $argNum ;  $i ++  )
    for  (  $i   =   0 ;  $i   <   $argNum ;  $i ++  )
30 {
  {
31 $_arg_ { $i }  =   func_get_arg (  $i  );
    $_arg_ { $i }  =   func_get_arg (  $i  );
32 }
  }
33 switch  (  $argNum  )
   switch  (  $argNum  )
34 {
  {
35 case   1 :
    case   1 :
36 testOne(  $_arg_1  );
    testOne(  $_arg_1  );
37 break ;
     break ;
38 case   2 :
    case   2 :
39 testTwo(  $_arg_1   ,   $_arg_2  );
    testTwo(  $_arg_1   ,   $_arg_2  );
40 break ;
     break ;
41 case   3 :
    case   3 :
42 testThree(  $_arg_1   ,   $_arg_2   ,   $_arg_3  );
    testThree(  $_arg_1   ,   $_arg_2   ,   $_arg_3  );
43 break ;
     break ;
44 default :
    default :
45 echo (  ' 这是没有参数的情况 '  );
     echo (  ' 这是没有参数的情况 '  );
46 break ;
     break ;
47 }
  }
48 }
 }
49
50 /* *
  /* *
51 * 例子的实现
  * 例子的实现
52 *
  *
53 */
   */
54 test();
 test();
55 echo (  ' <br> '  );
  echo (  ' <br> '  );
56 test( 1 );
 test( 1 );
57 echo (  ' <br> '  );
  echo (  ' <br> '  );
58 test( 1 , 2 );
 test( 1 , 2 );
59 echo (  ' <br> '  );
  echo (  ' <br> '  );
60 test( 1 , 2 , 3 );
 test( 1 , 2 , 3 );
61
62
63
64 // 这些只是在函数中的运用,其实最主要的还是在类中的运用
 // 这些只是在函数中的运用,其实最主要的还是在类中的运用
65 //如果这些用到类里面我就不需要担心构造函数是否有几个参数了,不是吗?
//如果这些用到类里面我就不需要担心构造函数是否有几个参数了,不是吗?
66 //类里面的运用只举一个简单的例子
//类里面的运用只举一个简单的例子
67
68 class  test
  class  test
69 {
 {
70 var   $a   =   0 ;
   var   $a   =   0 ;
71 var   $b   =   0 ;
   var   $b   =   0 ;
72
73 function  test ()
   function  test ()
74 {
  {
75 $argNum   =   func_num_args ();
    $argNum   =   func_num_args ();
76 $_arg   =   func_get_args ();
    $_arg   =   func_get_args ();
77 switch  (  $argNum  )
    switch  (  $argNum  )
78 {
   {
79 case   1 :
     case   1 :
80 $this -> test1(  $_arg [ 0 ] );
      $this -> test1(  $_arg [ 0 ] );
81 break ;
      break ;
82 case   2 :
     case   2 :
83 $this -> test2(  $_arg [ 0 ]  ,   $_arg [ 1 ]);
      $this -> test2(  $_arg [ 0 ]  ,   $_arg [ 1 ]);
84 break ;
      break ;
85 default :
     default :
86 $this -> a  =   0 ;
      $this -> a  =   0 ;
87 $this -> b  =   1 ;
      $this -> b  =   1 ;
88 break ;
      break ;
89 }
   }
90 }
  }
91
92 function  test1 (  $a  )
   function  test1 (  $a  )
93 {
  {
94 $this -> a  =   $a ;
    $this -> a  =   $a ;
95 }
  }
96
97 function  test2 (  $a   ,   $b )
   function  test2 (  $a   ,   $b )
98 {
  {
99 $this -> a  =   $a ;
    $this -> a  =   $a ;
100 $this -> b  =   $b ;
    $this -> b  =   $b ;
101 }
  }
102 }
 }
103
104
105 ?>
 ?>
转自: http://www.cnitblog.com/neatstudio/archive/2006/07/21/13949.html
  
  2

3

4
 // 今天在看书的时候,发现书上有这么一条:函数重载的替代方法————伪重载
 // 今天在看书的时候,发现书上有这么一条:函数重载的替代方法————伪重载5
 //
//6
 //确实,在PHP中没有函数重载这个概念,让很多时候我们无法进行一些处理,甚至有时候不得不在函数后面定义好N个参数
//确实,在PHP中没有函数重载这个概念,让很多时候我们无法进行一些处理,甚至有时候不得不在函数后面定义好N个参数7
 //在看到了func_get_arg,func_get_args,func_num_args,这三个函数的时候,你们是不是想起了什么?
//在看到了func_get_arg,func_get_args,func_num_args,这三个函数的时候,你们是不是想起了什么?8
 //SO,let's try it
//SO,let's try it ..
..9

10
 function  testOne (  $a  )
  function  testOne (  $a  )11
 {
 {12
 echo (  ' 一个参数就这样 '  );
   echo (  ' 一个参数就这样 '  );13
 }
 }14

15
 function  testTwo (  $a   ,   $b )
  function  testTwo (  $a   ,   $b )16
 {
 {17
 echo (  ' 两个参数的就这样 '  );
   echo (  ' 两个参数的就这样 '  );18
 }
 }19

20
 function  testThree (  $a   ,   $b   ,   $c )
  function  testThree (  $a   ,   $b   ,   $c )21
 {
 {22
 echo (  ' 黑黑,这是三个参数的 '  );
   echo (  ' 黑黑,这是三个参数的 '  );23
 }
 }24

25
 function  test ()
  function  test ()26
 {
 {27
 $argNum   =   func_num_args ();
   $argNum   =   func_num_args ();28
 // 这一段其实可以用 $_arg = func_get_args() 来获得所有的参数,只是要用数组而已,不方便我下面的表达,呵呵
   // 这一段其实可以用 $_arg = func_get_args() 来获得所有的参数,只是要用数组而已,不方便我下面的表达,呵呵29
 for  (  $i   =   0 ;  $i   <   $argNum ;  $i ++  )
    for  (  $i   =   0 ;  $i   <   $argNum ;  $i ++  )30
 {
  {31
 $_arg_ { $i }  =   func_get_arg (  $i  );
    $_arg_ { $i }  =   func_get_arg (  $i  );32
 }
  }33
 switch  (  $argNum  )
   switch  (  $argNum  )34
 {
  {35
 case   1 :
    case   1 :36
 testOne(  $_arg_1  );
    testOne(  $_arg_1  );37
 break ;
     break ;38
 case   2 :
    case   2 :39
 testTwo(  $_arg_1   ,   $_arg_2  );
    testTwo(  $_arg_1   ,   $_arg_2  );40
 break ;
     break ;41
 case   3 :
    case   3 :42
 testThree(  $_arg_1   ,   $_arg_2   ,   $_arg_3  );
    testThree(  $_arg_1   ,   $_arg_2   ,   $_arg_3  );43
 break ;
     break ;44
 default :
    default :45
 echo (  ' 这是没有参数的情况 '  );
     echo (  ' 这是没有参数的情况 '  );46
 break ;
     break ;47
 }
  }48
 }
 }49

50
 /* *
  /* *51
 * 例子的实现
  * 例子的实现52
 *
  *53
 */
   */54
 test();
 test();55
 echo (  ' <br> '  );
  echo (  ' <br> '  );56
 test( 1 );
 test( 1 );57
 echo (  ' <br> '  );
  echo (  ' <br> '  );58
 test( 1 , 2 );
 test( 1 , 2 );59
 echo (  ' <br> '  );
  echo (  ' <br> '  );60
 test( 1 , 2 , 3 );
 test( 1 , 2 , 3 );61

62

63

64
 // 这些只是在函数中的运用,其实最主要的还是在类中的运用
 // 这些只是在函数中的运用,其实最主要的还是在类中的运用65
 //如果这些用到类里面我就不需要担心构造函数是否有几个参数了,不是吗?
//如果这些用到类里面我就不需要担心构造函数是否有几个参数了,不是吗?66
 //类里面的运用只举一个简单的例子
//类里面的运用只举一个简单的例子67

68
 class  test
  class  test69
 {
 {70
 var   $a   =   0 ;
   var   $a   =   0 ;71
 var   $b   =   0 ;
   var   $b   =   0 ;72

73
 function  test ()
   function  test ()74
 {
  {75
 $argNum   =   func_num_args ();
    $argNum   =   func_num_args ();76
 $_arg   =   func_get_args ();
    $_arg   =   func_get_args ();77
 switch  (  $argNum  )
    switch  (  $argNum  )78
 {
   {79
 case   1 :
     case   1 :80
 $this -> test1(  $_arg [ 0 ] );
      $this -> test1(  $_arg [ 0 ] );81
 break ;
      break ;82
 case   2 :
     case   2 :83
 $this -> test2(  $_arg [ 0 ]  ,   $_arg [ 1 ]);
      $this -> test2(  $_arg [ 0 ]  ,   $_arg [ 1 ]);84
 break ;
      break ;85
 default :
     default :86
 $this -> a  =   0 ;
      $this -> a  =   0 ;87
 $this -> b  =   1 ;
      $this -> b  =   1 ;88
 break ;
      break ;89
 }
   }90
 }
  }91

92
 function  test1 (  $a  )
   function  test1 (  $a  )93
 {
  {94
 $this -> a  =   $a ;
    $this -> a  =   $a ;95
 }
  }96

97
 function  test2 (  $a   ,   $b )
   function  test2 (  $a   ,   $b )98
 {
  {99
 $this -> a  =   $a ;
    $this -> a  =   $a ;100
 $this -> b  =   $b ;
    $this -> b  =   $b ;101
 }
  }102
 }
 }103

104

105
 ?>
 ?>转自: http://www.cnitblog.com/neatstudio/archive/2006/07/21/13949.html
 相关阅读 更多 + 
    
  









