Summary and use of magic methods in PHP _ PHP Tutorial

Source: Internet
Author: User
Summary and examples of magic methods in PHP. Summary and examples of magic methods in PHP this article mainly introduces the summary and examples of magic methods in PHP. the magic method is a special feature of PHP object-oriented, summary and examples of their magic methods in specific situations in PHP

This article mainly introduces the summary and examples of the magic methods in PHP. the magic method is a special feature of PHP object-oriented, which is triggered under specific circumstances and starts with a double underline, you can understand them as Hooks. For more information, see

The magic method is a special feature of PHP object-oriented. They are triggered under specific circumstances. they all start with Double underscores. you can think of them as hooks, using the pattern method, you can easily overload PHP object-oriented objects (Overloading is the dynamic creation of class attributes and methods ). There are many magic methods in pairs. The following lists all the current PHP mode methods.

1. _ construct ,__ destruct

_ Constuct is called when constructing an object;

_ Destruct is called to explicitly destroy an object or end the script;

2. _ get ,__ set

_ Set is called when an inaccessible or non-existent attribute is assigned a value.

_ Get is called when reading an inaccessible or non-existent attribute

3. _ isset ,__ unset

_ Isset is called when isset () or empty () is called for inaccessible or non-existent attributes

_ Unset is called when an unset operation is performed on an inaccessible or non-existent attribute.

4. _ call ,__ callStatic

_ Call is called when a method that is inaccessible or does not exist

_ CallStatic is called when a static method that is inaccessible or does not exist

5. _ sleep ,__ wakeup

_ Sleep is called when serialize is used. it is useful when you do not need to save all data of large objects.

_ Wakeup is called when unserialize is used. it can be used to initialize objects.

6. _ clone

Called to adjust the cloning behavior of an object.

7. _ toString

Called when a class is converted to a string

8. _ invoke

Called when an object is called in function mode

9. _ set_state

This static method is called when var_export () is called to export the class. Use the return value of _ set_state as the return value of var_export.

10. _ debuginfo

It is called when var_dump () is called to print an object (when you do not want to print all attributes) for PHP5.6

Example of PHP magic method:

?

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

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

Class Magic

{

Public $ var = 'test ';

// Constructor called when an object is created

Public function _ construct ()

{

Echo '_ construct called'. PHP_EOL;

}

// All references to an object are deleted, the object is destroyed, and after exit () is called, or when the script is closed

Public function _ destruct ()

{

Echo '_ destruct called'. PHP_EOL;

}

// Called when a value is assigned to an inaccessible or non-existent attribute

Public function _ set ($ name, $ value)

{

Echo $ name. '-'. $ value;

Echo '_ set called'. PHP_EOL;

}

// It is called to read inaccessible or non-existent attributes.

Public function _ get ($ name)

{

Echo $ name;

Echo '_ get called'. PHP_EOL;

}

// Call a method that is inaccessible or does not exist

Public function _ call ($ name, $ arguments)

{

Echo $ name. '-'. implode (',', $ arguments );

Echo '_ call called'. PHP_EOL;

}

// Call an inaccessible or non-existent static method

Public static function _ callStatic ($ name, $ arguments)

{

Echo $ name. '-'. implode (',', $ arguments );

Echo '_ callStatic called'. PHP_EOL;

}

// Isset () or empty () is called when the attribute is inaccessible or does not exist.

Public function _ isset ($ name)

{

Echo $ name;

Echo '_ isset called'. PHP_EOL;

Return true;

}

// Called When unset is performed on inaccessible or non-existent attributes

Public function _ unset ($ name)

{

Echo $ name;

Echo '_ unset called'. PHP_EOL;

}

// It is called when serialize is used. it is useful when you do not need to save all data of a large object.

Public function _ sleep ()

{

Echo '_ sleep called'. PHP_EOL;

Return array ('var11111111111111 ');

}

// Unserialize is called and can be used to initialize objects.

Public function _ wakeup ()

{

Echo '_ wakeup called'. PHP_EOL;

$ This-> var = 'Test after wakeup ';

}

// Called when a class is converted to a string

Public function _ toString ()

{

Return '_ toString called'. PHP_EOL;

}

// Called for object cloning to adjust the object cloning behavior

Public function _ clone ()

{

Echo '_ clone called'. PHP_EOL;

}

// Called when an object is called in function mode

Public function _ invoke ()

{

Echo '_ invoke called'. PHP_EOL;

}

// This static method is called when var_export () is called to export the class. Use the return value of _ set_state as the return value of var_export.

Public static function _ set_state ($ arr)

{

Return '_ set_state called'. PHP_EOL;

}

// It is called when var_dump () is called to print the object (when you do not want to print all attributes) applicable to PHP5.6

Public function _ debuginfo ($ arr)

{

Echo '_ debuginfo called'. PHP_EOL;

Return array (

'Var' => 'test fro _ debuginfo'

);

}

}

$ M = new Magic (); // _ construct () called

$ M-> not_exist_property = test; // _ set () is called

Echo $ m-> not_exist_property; // _ get () called

$ M-> abc (1, 2, 3); // _ call () is called

Echo isset ($ m-> not_exist_property); // _ isset () is called, return the bool value

Unset ($ m-> not_exist_property); // _ unset () is called

Echo $ tmp = serialize ($ m); // _ sleep () is called

Unserialize ($ tmp); // _ wakeup () is called

$ M1 = clone $ m; // _ clone () is called. objects are passed by reference by default. object replication can be implemented using the clone keyword.

$ M (); // _ invoke ()

Eval ('$ m2 ='. var_export ($ m, true). ';'); var_dump ($ m2 );

Var_dump ($ m );

// The Last _ destruct () is called.

/*

Result:

_ Construct called

Not_exist_property-test _ set called

Not_exist_property _ get called

ABC-1, 2,3 _ call called

Not_exist_property _ isset called

1not_exist_property _ unset called

_ Sleep called

O: 5: "Magic": 1: {s: 13: "var1111111111"; N ;:__ wakeup called

_ Destruct called

_ Clone called

_ Invoke called

String (20) "_ set_state called

"

Class Magic #1 (1 ){

Public $ var =>

String (4) "test"

}

_ Destruct called

_ Destruct called

*/

This article mainly introduces the summary and examples of magic methods in PHP. magic methods are a special feature of PHP object-oriented, they are specific in the specific situation...

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.