From the PHP 5 version, the class in PHP can use the Magic method. The rules that start with two underscores (__) are reserved as magic methods, so it is recommended that you do not start with a function name unless you are trying to overload an existing magic method. PHP leaves all class methods that begin with _ _ (two underscores) as magic methods.
__tostring () and __invoke ()
public string __tostring (void): This method is invoked automatically when an object is used as a string. This method must return a string
Copy Code code as follows:
<?php
Class magic{
Public Function __tostring () {
Return "Hello world!";
}
}
$obj = new Magic ();
echo $obj;//hello world!
?>
__invoke (): This method is invoked automatically when the object is called as a method.
Copy Code code 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 (a)//__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 invoked
__callstatic (): When an object accesses a static method name that does not exist, the __callstatic () method is automatically invoked
By using these two methods, the invocation of the name of the same method can be implemented in a different way
Copy Code code as follows:
<?php
Class magic{
The ' $name ' parameter is the name of the method to invoke. The ' $arguments ' argument is an enumerated array,
Contains the arguments to pass to the method ' $name '.
Public Function __call ($name, $arguments) {
The implode () function combines array elements into a single string. Implode (Separator,array)
echo "Calling". $name. "With Param:". Implode (",", $arguments). " \ n ";
}
}
$obj = new Magic ();
$obj->run ("Para1", "Para2");//obj to invoke the Run method, output: Calling run with Param:para1, PARA2
?>
__get () and __set ()
When you assign a value to an inaccessible property, __set () is called
When reading the value of an inaccessible property, __get () is called
Copy Code code as follows:
<?php
Class magic{
A function must have a static keyword before it
Public Function __get ($name) {
Return "getting". $name;
}
}
$obj = new Magic ();
echo $obj->classname. " \ n ";//getting the property ClassName
?>
When reading the value of an inaccessible property, __get () is called
Copy Code code 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 invoked on an inaccessible property, __isset () is called
When unset () is invoked on an inaccessible property, __unset () is called
Copy Code code 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 the 8 PHP object-oriented Magic Method Introduction and examples, I hope to be helpful to everyone