PHP function overloading Alternative Methods _php Tutorial

Source: Internet
Author: User

Workarounds for PHP function overloading


For weakly typed languages, PHP function overloading is not as normal as OOP. Weak types are inherently indistinguishable, so they cannot be implemented. However, it is possible to implement a simple pseudo-overload by using the following method.

Workaround for function overloading-pseudo-overloading, see a specific instance code below.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

Workaround for function overloading-pseudo overload

//

Indeed, there is no concept of function overloading in PHP, so many times we are unable to do some processing, and sometimes we have to define N parameters after the function.

What do you think of when you see Func_get_arg,func_get_args,func_num_args, these three functions?

function Testone ($a) {

Echo (' A parameter just so ');

}

function Testtwo ($a, $b) {

Echo (' Two parameters in this case ');

}

function Testthree ($a, $b, $c) {

Echo (' Black black, this is three parameters ');

}

function Test () {

$argNum = Func_num_args ();

This paragraph can actually use $_arg = Func_get_args () to get all the parameters, just to use the array, not convenient my following expression, hehe

for ($i = 0; $i < $argNum; $i + +) {

$_arg_ {$i} = Func_get_arg ($i);

}

Switch ($argNum) {

Case 1:

Testone ($_arg_1);

break;

Case 2:

Testtwo ($_arg_1, $_arg_2);

break;

Case 3:

Testthree ($_arg_1, $_arg_2, $_arg_3);

break;

Default:

Echo (' This is the case without parameters ');

break;

}

}

Test ();

Echo (');

Test (1);

Echo (');

Test (1, 2);

Echo (');

Test (1, 2, 3);

These are only used in the function, in fact, the most important or in the application of the class

If this is used in a class, I don't have to worry about whether the constructor has a few arguments, does it?

Class Test {

var $a = 0;

var $b = 0;

function Test () {

$argNum = Func_num_args ();

$_arg = Func_get_args ();

Switch ($argNum) {

Case 1:

Test1, $this ($_arg [0]);

break;

Case 2:

$this-Test2 ($_arg [0], $_arg [1]);

break;

Default:

$this a = 0;

$this B = 1;

break;

}

}

function Test1 ($a) {

$this a = $a;

}

function test2 ($a, $b) {

$this a = $a;

$this B = $b;

}

}

?>

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/996752.html www.bkjia.com true http://www.bkjia.com/PHPjc/996752.html techarticle PHP function overloading alternative methods for weakly typed languages, PHP function overloading is not as normal as OOP. Weak types are inherently indistinguishable, so they cannot be implemented. But ... ..

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.