Summary and usages of magic methods in PHP

Source: Internet
Author: User
Tags export class print object

This article mainly introduces the magic methods in PHP summary and use of examples, the Magic method is the unique characteristics of the PHP object-oriented, they are triggered in a specific case, are the beginning of a double underline, you can interpret them as hooks, need friends can refer to the

The Magic method is unique to PHP's object-oriented features. They are triggered in a particular case, they start with a double underscore, you can interpret them as hooks, and you can easily implement the PHP object-oriented overload using the pattern method (overloading), which dynamically creates class properties and methods. Many of the magic methods appear in pairs, and the following lists all of the current mode methods in PHP.

1.__construct,__destruct

__constuct is invoked when the object is constructed;

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

2.__get,__set

__set is invoked when assigning values to inaccessible or non-existent attributes

__get is invoked when a property is read that is inaccessible or does not exist

3.__isset,__unset

__isset is called when isset () or empty () is invoked on a property that is inaccessible or nonexistent.

__unset is invoked when unset to an inaccessible or nonexistent property

4.__call,__callstatic

Called when an __call or nonexistent method is invoked

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

5.__sleep,__wakeup

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

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

6.__clone

Invoked when object clone is used to adjust the clone behavior of an object

7.__tostring

Called when a class is converted to a string

8.__invoke

Called when an object is invoked as a function

9.__set_state

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

10.__debuginfo

Called when the Var_dump () Print object is invoked (when you do not want to print all properties) apply to the PHP5.6 version

The PHP Magic method uses the following example:

?

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 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 5, 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 11 9 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 <?php   class Magic {public $var = ' test '  /constructor, calling public function __construct () {echo ' __cons when creating an object Truct called '. Php_eol;  //A reference to an object is deleted, the object is destroyed, the call to exit () is invoked, and the public Function __destruct () {echo ' __destruct called ') is called when the script closes. Php_eol;   The Public Function __set ($name, $value) {echo $name. '-' $value; Echo ' __set called ' when assignment to an inaccessible or nonexistent property is invoked. Php_eol; The Public Function __get ($name) {echo $name, echo ' __get called ') is invoked when reading inaccessible or non-existent attributes. \  / Php_eol;   The Public Function __call ($name, $arguments) {echo $name is invoked when an inaccessible or nonexistent method is invoked. '-' . Implode (', ', $arguments); Echo ' __call called '. Php_eol;   The public static function __callstatic ($name, $arguments) {echo $name is invoked when a static method that is unreachable or nonexistent is invoked. '-' . Implode (', ', $arguments); Echo ' __callstatic called '. Php_eol;   The Public Function __isset ($name) {echo $name; Echo ' __isset called ') is invoked when isset () or empty () is invoked on an inaccessible or Non-existent property. Php_eol; return true; The Public Function __unset ($name) {EC is invoked when unset of an inaccessible or nonexistent property is performed  //Ho $name; Echo ' __unset called '. Php_eol; When  //serialize is invoked, it is useful when you do not need to save all the data for large objects public Function __sleep () {echo ' __sleep called '. Php_eol; Return Array (' var1111111111 '); }  //unserialize is invoked to do initialization of some objects public function __wakeup () {echo ' __wakeup called '. Php_eol; $this->var = ' test after wakeup ';  //When a class is converted to a string, the public function __tostring () {return ' __tostring called ' is invoked. Php_eol; The  //is invoked when object cloning is used to adjust the clone behavior of the object public function __clone () {echo ' __clone called '. Php_eol;  //When calling an object as a function, the public function __invoke () {echo ' __invoke called ') is invoked. Php_eol; This static method is invoked when the Var_export () export class is called by  //. Use the return value of the __set_state as the return value of the Var_export. public static function __set_state ($arr) {return ' __set_state called '. Php_eol;  //When calling Var_dump () to print an object (when you do not want to print all properties) is applied to 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 () called Echo $m->not_exist_property;//__get () is invoked $m->ABC (1,2,3); __call () is invoked to echo isset ($m->not_exist_property); __isset () is invoked to return the bool value unset ($m->not_exist_property); __unset () is invoked to echo $tmp = serialize ($m); __sleep () is called unserialize ($tmp); __wakeup () is called $m 1 = clone $m; __clone () is invoked, the object is referenced by default, and using the Clone keyword enables the object to replicate $m (); __invoke () eval (' $m 2 = '. Var_export ($m, True). ';' ); Var_dump ($m 2); Var_dump ($m); Last __destruct () 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 (m) "__set_state called" Class Magic#1 (1) {Publ IC $var => string (4) "test"} __destruct called __destruct called     *
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.