PHP Magic Methods Usage summary, php magic summary _ PHP Tutorial

Source: Internet
Author: User
PHP magic method usage summary, php magic summary. PHP magic methods use method summary, php magic summary magic method is a special feature of PHP object-oriented. They are triggered under specific circumstances. they all start with a double underline. you can summarize PHP magic methods and php magic methods.

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:

<? Php class Magic {public $ var = 'test'; // Constructor. call public function _ construct () {echo '_ construct called' when creating an object '. PHP_EOL;} // a reference to an object is deleted, the object is destroyed, exit () is called, and public function _ destruct () is called when the script is closed () {echo '_ destruct called '. PHP_EOL;} // public function _ set ($ name, $ value) {echo $ name. '-'. $ value; echo '_ set called '. PHP_EOL;} // public function _ get ($ name) {echo $ Name; echo '_ get called '. PHP_EOL;} // public function _ call ($ name, $ arguments) {echo $ name. '-'. implode (',', $ arguments); echo '_ call called '. PHP_EOL;} // public static function _ callStatic ($ name, $ arguments) {echo $ name. '-'. implode (',', $ arguments); echo '_ callStatic called '. PHP_EOL;} // public function _ isset is called when isset () or empty () is called for an inaccessible or non-existent attribute. ($ Name) {echo $ name; echo '_ isset called '. PHP_EOL; return true;} // public function _ unset ($ name) {echo $ name; echo '_ unset called '. PHP_EOL;} // serialize is called. public function _ sleep () {echo '_ sleep called' is useful when you do not need to save all data of a large object '. PHP_EOL; return array ('var11111111111111');} // unserialize is called and can be used to initialize some objects. public function _ wakeup () {echo '_ wakeup called '. PHP_EOL; $ this-> Var = 'Test after wakeup ';} // when a class is converted to a string, public function _ toString () {return' _ toString called 'is called '. PHP_EOL;} // called when cloning an object. it is used to adjust the object's clone behavior public function _ clone () {echo '_ clone called '. PHP_EOL;} // public function _ invoke () {echo '_ invoke called' is called when an object is called in function mode '. 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;} // This is called when var_dump () is called to print an object (when you do not want to print all attributes). This applies to public function _ debuginfo ($ arr) of PHP5.6) {echo '_ debuginfo called '. PHP_EOL; return array ('var' => 'test fro _ debuginfo '); }}$ m = new Magic (); // _ construct () called $ m-> not_exist_property = test; // _ set () echo $ m-> not_exist_property; // _ get () $ m-> abc (1, 2, 3); // _ call () is called echo isset ($ m-> not_exist_property); // _ isset () called, return 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 () called. the object is passed by reference by default. you can use the clone keyword to copy $ m (); // _ invoke () eval ('$ m2 = '. var_export ($ m, true ). ';'); var_dump ($ m2); var_dump ($ m); // The Last _ destruct () is 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: "var11111111"; N ;:__ wakeup called _ destruct called _ clone called _ invoke calledstring (20) "_ set_state called" class Magic #1 (1) {public $ var => string (4) "test" }__ destruct called _ destruct called */

The above is all about how to use PHP magic methods. I hope it will be helpful for your learning.

Articles you may be interested in:
  • Php magic methods
  • Describes how to use php magic methods _ get () and _ set ()
  • PHP5 magic constants and magic methods
  • In-depth explanation of magic methods in PHP5
  • Summary of common magic methods in php
  • Php object-oriented magic methods
  • Magic methods in ThinkPHP query
  • Magic Method in PHP
  • How to add magic method__ invoke in PHP 5.3
  • PHP magic method _ GET, _ SET use instance

The ghost magic method is a special feature of PHP object-oriented. They are triggered in a specific situation. they all start with a double underline...

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.