Test various PHP magic methods

Source: Internet
Author: User
1 <? PHP 2 # automatic loading method. When a new class is added, if the class is not introduced, the _ autoload () method 3 function _ autoload ($ classname) is automatically called) {4 include $ classname. ". PHP "; 5} 6 7 class son {8 private $ name; 9 10 public function _ construct ($ name) {11 $ this-> name = $ name; 12} 13 14 # automatically call this method when ECHO outputs the class 15 public function _ tostring () {16 return $ this-> name; 17} 18} 19 20 class man {21 private $ name; 22 private $ age; 23 private $ son; 24 25 Public Function _ construct ($ name, $ age, son $ son) {26 $ this-> name = $ name; 27 $ this-> age = $ age; 28 $ this-> son = $ son; 29} 30 31 # destructor. This method is executed when the script ends or unset () is called. 32 public function _ destruct () {33 echo "destruct: I'm {$ this-> name}, I was killed ". date ("Y-m-d h: I: s "). "<br>"; 34} 35 36 public function _ tostring () {37 return "tostring: I'm {$ this-> name }, and {$ this-> Age} years old, my son I S {$ this-> son }. <br> "; 38} 39 40 # This method is automatically called when a nonexistent function is called. 41 public function _ call ($ funcname, $ ARGs) {42 echo "Call: You call func \" {$ funcname} \ "with ARGs \"". serialize ($ ARGs ). "\"? It's not existed. <br> "; 43} 44 45 # This method is automatically called when a nonexistent static function is called. 46. Public static function _ callstatic ($ funcname, $ ARGs) {47 echo "callstatic: You call static func \" {$ funcname} \ "with ARGs \"". serialize ($ ARGs ). "\"? It's not existed. <br> "; 48} 49 50 # This method is automatically called when get is called and $ varname does not exist. 51 public function _ Get ($ varname) {52 return" get: you call the variable \ "{$ varname }\"? It's not existed. <br> "; 53} 54 55 # This method is automatically called when set is called and $ varname does not exist. 56 public function _ set ($ varname, $ Val) {57 echo "set: You want to assign value \" {$ Val} \ "to variable \" {$ varname }\"? It's not existd. <br> "; 58} 59 60 # This method is automatically called when isset is called and $ varname does not exist. 61 public function _ isset ($ varname) {62 echo" isset: tell you, the variable \ "{$ varname} \" is not existed. <br> "; 63} 64 65 # This method is automatically called when unset is called and $ varname does not exist. 66 public function _ unset ($ varname) {67 echo" Unset: tell you, the variable \ "{$ varname} \" has never been existed. <br> "; 68} 69 70 # execute this method before calling serialize (). serialize () Only serializes _ systemic 71 public function _ sleep () {72 return array ("name"); 73} 74 75 # Before unserialize () is called, the 76 public function _ wakeup () {77 $ this-> age = "21"; 78} 79 80 # When the class is called as a function, will execute this method 81 public function _ invoke ($ var) {82 echo "you want to invoke class \"". _ class __. "\" as a func with ARGs \ "{$ var }\"? No! That's wrong! <Br> "; 83} 84 85 # This method will be executed when clone is called 86 public function _ clone () {87 $ this-> son = clone $ this-> son; # perform a deep copy here to force a new copy of the object type member variable, otherwise, just copy the object reference 88} 89} 90 91 $ man = new man ("Jack", 18, new son ("dick ")); 92 $ man-> smoke ("camle cigarette"); # test _ call () 93 man: Drink (Array ("juice", "water ")); # test _ callstatic () 94 echo $ man-> wife; # test _ Get () 95 $ man-> wife = "Rose"; # test _ set () 96 isset ($ man-> daughter); # test __ Isset () 97 unset ($ man-> daughter); # test _ unset () 98 echo serialize ($ man ). "<br>"; # test__sleep () 99 echo unserialize (serialize ($ man); # test__ wakeup () 100 $ man ("kiss "); # test _ invoke () 101 $ mannew = clone $ man; # test _ clone () 102 echo $ mannew; 103 $ woman = New Woman (); # test__ autoload () 104?>

Page output

Call: You call func "smoke" with ARGs "A: 1: {I: 0; s: 15:" camle cigarette ";}"? It's not existed.
Callstatic: You call static func "drink" with ARGs "A: 1: {I: 0; A: 2: {I: 0; s: 5:" juice "; I: 1; s: 5: "water ";}}"? It's not existed.
Get: You call the variable "wife "? It's not existed.
Set: You want to assign value "Rose" to variable "wife "? It's not existd.
Isset: Tell you, the variable "Daughter" is not existed.
Unset: Tell you, the variable "Daughter" has never been existed.
O: 3: "Man": 1: {s: 9: "manname"; s: 4: "Jack ";}
Tostring: I'm Jack, and 21 years old, my son is.
Destruct: I'm Jack, I was killed at 20:00:34
You want to invoke class "man" as a func with ARGs "kiss "? No! That's wrong!
Destruct: I'm Jack, I was killed at 20:00:34

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.