This article introduces several magic methods in php object-oriented language and describes them in detail, hoping to help new users. In PHP versions later than PHP 5, classes in PHP can use magic methods. It specifies that all methods starting with two underscores (_) are retained as magic methods. Therefore, we recommend that you do not start with _ in the function name unless you want to reload the existing magic methods. PHP retains all class methods starting with _ (two underscores) as magic methods.
_ ToString () and _ invoke ()
Public string _ toString (void): This method is automatically called when an object is used as a string. This method must return a string
The code is as follows:
<? Php
Class Magic {
Public function _ tostring (){
Return "hello world! ";
}
}
$ Obj = new Magic ();
Echo $ obj; // hello world!
?>
_ Invoke (): This method is automatically called when an object is called as a method.
The code is as follows:
<? Php
Class Magic {
Public function _ tostring (){
Return "hello world! ";
}
Public function _ invoke ($ x ){
Echo "_ invoke called with param". $ x. "\ n ";
}
}
$ Obj = new Magic ();
$ Obj (10); // _ invoke called with param 10
?>
_ Call () and _ callStatic ()
_ Call (): When an object accesses a method name that does not exist, the __call () method is automatically called.
_ CallStatic (): When the object accesses a static method name that does not exist, the __callstatic () method is automatically called.
Through these two methods, the call of the same method name can correspond to different method implementations
The code is as follows:
<? Php
Class Magic {
// The '$ name' parameter is the name of the method to be called. The '$ arguments' parameter is an enumerated array,
// Contains the parameter to be passed to the method '$ name.
Public function _ call ($ name, $ arguments ){
// The implode () function combines array elements into a string. Implode (separator, array)
Echo "Calling". $ name. "with param:". implode (",", $ arguments). "\ n ";
}
}
$ Obj = new Magic ();
$ Obj-> run ("para1", "para2"); // obj calls the run method. output: Calling run with param: para1, para2
?>
_ Get () and _ set ()
When assigning values to inaccessible properties, __set () will be called
When you read the value of an inaccessible attribute, __get () will be called
The code is as follows:
<? Php
Class Magic {
// There must be a static keyword before the function
Public function _ get ($ name ){
Return "Getting the property". $ name;
}
}
$ Obj = new Magic ();
Echo $ obj-> className. "\ n"; // Getting the property className
?>
When you read the value of an inaccessible attribute, __get () will be called
The code is as follows:
<? Php
Class Magic {
Public function _ set ($ name, $ value ){
Echo "Setting the property". $ name. "to value". $ value. "\ n ";
}
}
$ Obj = new Magic ();
$ Obj-> className = 'magicclass'; // Setting the property classNameto value MagicClass
?>
_ Isset () and _ unset ()
When isset () or empty () is called for an inaccessible attribute, __isset () is called.
When unset () is called for an inaccessible attribute, __unset () is called.
The code is as follows:
<? Php
Class Magic {
Public function _ isset ($ name ){
Echo "_ isset invoked \ n ";
Return true;
}
}
$ Obj = new Magic ();
Echo '$ obj-> className is set? '. Isset ($ obj-> className). "\ n"; // _ isset invoked $ obj-> className is set? 1
?>
The above is an introduction and example of eight php object-oriented magic methods, hoping to help you