This article mainly introduces the PHP magic method of __call and __callstatic How to use, the need for friends can refer to the next
Core code
Magic Method __call/* $method Get the method name $arg get the parameter collection of the method */class Human {private Function T () {} public Function __call ($method, $arg) { Echo ' You want to call the way I don't exist ', $method, ' method <br/> '; Echo ' also transmitted a parameter <br/> '; echo Print_r ($arg), ' <br/> '; } public static function __callstatic ($method, $arg) { echo ' you want to call me nonexistent ', $method, ' static method <br/> '; Echo ' also transmitted a parameter <br/> '; echo Print_r ($arg), ' <br/> '; }} $ha = new Human ();//example1$ha->t (n/a); Echo ' <br> ';//example2$ha->say (' A ', ' B ', ' C '); Echo ' <br> ' ;//example3$ha::run (' d ', ' e ', ' f ');
You want to call the way I don't exist t method
And a parameter was passed.
Array ([0] = 1 [1] = 2 [2] = 3)
You want to call the way I don't exist say method
And a parameter was passed.
Array ([0] = a [1] = b [2] = = c)
You want to call the run static method that I don't exist
And a parameter was passed.
Array ([0] = + d [1] = e [2] = f)
Related recommendations:
Common Magic Methods for PHP
"PHP Classes and Objects" Magic method
Research on the performance of common magic methods in PHP