Summary and use examples of magic methods in PHP _php tutorial

Source: Internet
Author: User
Tags export class print object

Summary and usage examples of magic methods in PHP


This article mainly introduces the Magic method in PHP summary and use examples, the Magic method is the PHP object-oriented features, they are triggered in a particular case, are to start with a double underscore, you can interpret them as hooks, the need for friends can refer to the next

The Magic method is a unique feature of PHP object-oriented. They are triggered under certain circumstances, all starting with a double underscore, you can interpret them as hooks, and the pattern method makes it easy to implement PHP object-oriented overloading (overloading, which dynamically creates class properties and methods). Many of the magic methods are still paired, and the following list shows all the schema methods in PHP currently.

1.__construct,__destruct

__constuct is called when the object is built;

__destruct explicitly destroys the object or is called at the end of the script;

2.__get,__set

__set called when an attribute assignment is not accessible or does not exist

__get is called when read is not accessible or there is no property

3.__isset,__unset

__isset called when isset () or empty () is called on an inaccessible or nonexistent property

__unset called when an unreachable or nonexistent property is unset

4.__call,__callstatic

Called when __call calls a method that is not accessible or does not exist

Called when __callstatic calls a static method that is not accessible or does not exist

5.__sleep,__wakeup

__sleep is called when using serialize, which is useful when you don't need to save all the data for a large object

__wakeup is called when using unserialize, and can be used for initialization of objects

6.__clone

Called when the object clone is being used to adjust the cloning behavior of the object

7.__tostring

Called when a class is converted to a string

8.__invoke

Called when an object is called in a functional manner

9.__set_state

This static method is called when the Var_export () export class is called. Use the return value of __set_state as the return value of Var_export.

10.__debuginfo

Called when a var_dump () print object is called (when you do not want to print all properties) for the PHP5.6 version

Examples of using the PHP magic method are:

?

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

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 that is called when the object is created

Public Function __construct ()

{

Echo ' __construct called '. Php_eol;

}

A reference to an object is deleted, the object is destroyed, a call to exit () is called, and the script is closed

Public Function __destruct ()

{

Echo ' __destruct called '. Php_eol;

}

Called when an attribute assignment is not accessible or does not exist

Public Function __set ($name, $value)

{

echo $name. '-' $value;

Echo ' __set called '. Php_eol;

}

Called when the read is not accessible or the property is not present

Public Function __get ($name)

{

Echo $name;

Echo ' __get called '. Php_eol;

}

Called when a method is called that is not accessible or does not exist

Public Function __call ($name, $arguments)

{

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

Echo ' __call called '. Php_eol;

}

Called when a static method is called that is not accessible or does not exist

public static function __callstatic ($name, $arguments)

{

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

Echo ' __callstatic called '. Php_eol;

}

Called when isset () or empty () is called for an unreachable or nonexistent property

Public Function __isset ($name)

{

Echo $name;

Echo ' __isset called '. Php_eol;

return true;

}

Called when an unset property is not accessible or not present

Public Function __unset ($name)

{

Echo $name;

Echo ' __unset called '. Php_eol;

}

Serialize is called when you don't need to save all the data for a large object.

Public Function __sleep ()

{

Echo ' __sleep called '. Php_eol;

Return Array (' var1111111111 ');

}

Unserialize is called when it is 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 when the object clone is being used to adjust the cloning behavior of the object

Public Function __clone ()

{

Echo ' __clone called '. Php_eol;

}

Called when an object is called in a functional manner

Public Function __invoke ()

{

Echo ' __invoke called '. Php_eol;

}

This static method is called when the Var_export () export class is called. 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;

}

Called when a var_dump () print object is called (when you do not want to print all properties) for the PHP5.6 version

Public Function __debuginfo ($arr)

{

Echo ' __debuginfo called '. Php_eol;

Return Array (

' var ' = ' Test fro __debuginfo '

);

}

}

$m = new Magic (); __construct () is called

$m->not_exist_property = test; __set () is called

echo $m->not_exist_property;//__get () is called

$m->abc (a); __call () is called

Echo isset ($m->not_exist_property); __isset () is called and returns a bool value

unset ($m->not_exist_property); __unset () is called

echo $tmp = serialize ($m); __sleep () is called

Unserialize ($TMP); __wakeup () is called

$m 1 = clone $m; __clone () is invoked, the object is passed by default, and the Clone keyword is used to implement object replication

$m (); __invoke ()

Eval (' $m 2 = '. Var_export ($m, True). ';' ); Var_dump ($m 2);

Var_dump ($m);

Last __destruct () is called

/*

Results:

__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 ("__set_state called")

"

Class Magic#1 (1) {

Public $var = =

String (4) "Test"

}

__destruct called

__destruct called

*/

http://www.bkjia.com/PHPjc/998363.html www.bkjia.com true http://www.bkjia.com/PHPjc/998363.html techarticle Summary and use examples of magic methods in PHP this article mainly introduces the Magic method in PHP summary and use examples, magic method is the PHP object-oriented features, they 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.