Php function overload substitution method _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php function overload substitution method. The alternative to php function overloading is not as common as OOP for weak-type languages. However, the weak types are not differentiated, so they cannot be implemented through these methods. But the php function overload alternative method

For a weak type language, PHP function overloading is not as common as OOP. However, the weak types are not differentiated, so they cannot be implemented through these methods. However, you can use the following method to implement simple pseudo-overload.

The replacement method of function overload-pseudo-overload. let's take a look at the specific instance code.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

// Alternative to function overload-pseudo-overload

//

// Indeed, there is no function overload concept in PHP, which makes it impossible for us to perform some processing in many cases. sometimes we have to define N parameters after the function.

// When you see func_get_arg, func_get_args, func_num_args, do you think of these three functions?

Function testOne ($ ){

Echo ('one parameter is like this ');

}

Function testTwo ($ a, $ B ){

Echo ('Two parameters are like this ');

}

Function testThree ($ a, $ B, $ c ){

Echo ('Black and black, these are three parameters ');

}

Function test (){

$ ArgNum = func_num_args ();

// You can use $ _ arg = func_get_args () to obtain all the parameters in this section. it is just an array. it is not convenient for me to express it below.

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 with no arguments ');

Break;

}

}

Test ();

Echo ('');

Test (1 );

Echo ('');

Test (1, 2 );

Echo ('');

Test (1, 2, 3 );

// These are only used in functions. In fact, they are mainly used in classes.

// If these classes are used, I don't need to worry about whether the constructor has several parameters, do I?

Class test {

Var $ a = 0;

Var $ B = 0;

Function test (){

$ ArgNum = func_num_args ();

$ _ Arg = func_get_args ();

Switch ($ argNum ){

Case 1:

$ This-> test1 ($ _ arg [0]);

Break;

Case 2:

$ This-> test2 ($ _ arg [0], $ _ arg [1]);

Break;

Default:

$ This-> a = 0;

$ This-> B = 1;

Break;

}

}

Function test1 ($ ){

$ This-> a = $;

}

Function test2 ($ a, $ B ){

$ This-> a = $;

$ This-> B = $ B;

}

}

?>

The above is all the content of this article. I hope you will like it.

For weak types of languages, the PHP function overload is not like the general OOP. However, the weak types are not differentiated, so they cannot be implemented through these methods. But...

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.