The definition and use of magic methods in PHP

Source: Internet
Author: User
Tags export class php class print object
This article mainly introduces the definition and use of magic methods in PHP, the Magic method is specific to 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:

<?php class magic{Public $var = ' Test '; constructor, call public function __construct () {echo ' __construct called ' when creating the object.  Php_eol; }//A reference to an object is deleted, the object is destroyed, a call to exit () is called, and the script closes with the public Function __destruct () {echo ' __destruct called '.  Php_eol;    The Public Function __set ($name, $value) {echo $name. '-') is called when the property assignment is not accessible or does not exist. $value; Echo ' __set called '.  Php_eol;    The Public Function __get ($name) {echo $name is called when the read is not accessible or the property is not present; Echo ' __get called '.  Php_eol; }//Call public function __call ($name, $arguments) {echo $name when calling an inaccessible or nonexistent method. '-' .    Implode (', ', $arguments); Echo ' __call called '.  Php_eol; The public static function __callstatic ($name, $arguments) {echo $name is called when a static method is called that is not accessible or does not exist. '-' .    Implode (', ', $arguments); Echo ' __callstatic called '.  Php_eol;    The Public Function __isset ($name) {echo $name is called when isset () or empty () is called on an inaccessible or nonexistent property; Echo ' __isset called '.    Php_eol;  return true; The public function is called when unset an unreachable or nonexistent property __unseT ($name) {echo $name; Echo ' __unset called '.  Php_eol; }//serialize is called when you do not need to save all the data for a large object, it is useful for public function __sleep () {echo ' __sleep called '.    Php_eol;  Return Array (' var1111111111 '); }//unserialize is called when it can be used to do some initialization of 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 called.  Php_eol; }//is called when the object clone is being used to adjust the cloning behavior of the object public function __clone () {echo ' __clone called '.  Php_eol; The Public Function __invoke () {echo ' __invoke called ' is called when the object is called in a functional manner.  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; }//When calling Var_dump () to print an object (when you do not want to print all properties) applies to the PHP5.6 version of 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-&GT;ABC (); __call () is called Echo isset ($m->not_exist_property); __isset () is called and returns a bool value of 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, the object is a reference pass by default, and the Clone keyword can be used to replicate the object $m (); __invoke () eval (' $m 2 = '. Var_export ($m, True). ';' ); Var_dump ($m 2); Var_dump ($m);//Last __destruct () called/* Result: __construct callednot_exist_property-test__set callednot_ Exist_property__get Calledabc-1,2,3__call callednot_exist_property__isset Called1not_exist_property__unset called_ _sleep calledo:5: "Magic": 1:{s:13: "var1111111111"; N;} __wakeup called__destruct called__clone called__invoke calledstring () "__set_state called" Class Magic#1 (1) {public $v AR = string (4) "test"}__destruct called__destruct called */

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP implements simple get, POST, Cookie, session and other functions

How PHP operates a database to determine if a table exists

How PHP Gets the file MIME type

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.