Continuing with the overloading mentioned last time, let’s learn about overloading in PHP and method overloading. If there is a definition of overloading, please refer to: Overloading in PHP (1). Thank you. As a beginner, Big cows, please don’t spray:
Basically there are two methods
__call, when calling a method on an inaccessible object, this magic method will be automatically executed! (Object call)
Two typical processing methods:
1. Give friendly tips!
2, perform the default operation!
__callstatic, when an inaccessible static method is called, this magic method will be automatically executed!
Detailed code:
class Student {
public $name = 'php';
public $age = 10;
public function sayName() {
return $this->name;
}
/**
* @param $method_name string method name
* @param $method_arguments array Parameters carried when calling
*/
public function __call($method_name, $method_arguments) {
echo '
Sorry, the method you called does not exist!, you should call the (***) method! ';
$this->defaultAction();//Execute the default method, which can be used for jumps and redirect
}
public function defaultAction() {
echo '
This is the default action! ';
}
public static function __callStatic($m_name, $m_args) {
echo 'This is static method overloading! ';
}
}
Student::sayCounter();
The above are examples for reference when studying. They are divided below... If you have any questions, we can discuss