Alternatives to php function overloading_PHP Tutorial

WBOY
Release: 2016-07-13 09:54:38
Original
964 people have browsed it

Alternatives to PHP function overloading

For weakly typed languages, PHP function overloading is not like general OOP. Weak types are not inherently differentiated, so they cannot be implemented through these. However, you can achieve simple pseudo-overloading through the following method.

An alternative to function overloading - pseudo-overloading. Let’s look at a specific example code below.

 ?

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

//An alternative to function overloading - pseudo-overloading

//

//Indeed, there is no concept of function overloading in PHP, which makes it impossible for us to perform some processing in many cases, and sometimes we even have to define N parameters behind the function

//When you saw these three functions, func_get_arg, func_get_args, and func_num_args, did you think of anything?

function testOne ( $a ) {

echo (' Just one parameter ');

}

function testTwo ( $a , $b ){

echo ('That’s it for two parameters');

}

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

echo ('Heihei, this is three parameters');

}

function test () {

$argNum = func_num_args ();

// In this paragraph, you can actually use $_arg = func_get_args() to get all the parameters, but you have to use an array, which is inconvenient for my expression below, haha

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 just applications in functions, in fact the most important thing is application in classes

//If these are used in the class, I don’t need to worry about whether the constructor has several parameters, right?

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 ($a) {

$this -> a = $a ;

}

function test2 ($a, $b) {

$this -> a = $a ;

$this -> b = $b ;

}

}

?>

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/996752.htmlTechArticleAlternatives to PHP function overloading For weakly typed languages, PHP function overloading is not like general OOP like that. Weak types are not inherently differentiated, so they cannot be implemented through these. But...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template