This article mainly introduces the Yii framework's use of magic methods to achieve cross-file calling functions, involving php object-oriented programming-related operating techniques in the Yii framework, friends in need can refer to it The following
example in this article describes how the Yii framework uses magic methods to implement cross-file calls. Share it with everyone for your reference, the details are as follows:
The current project uses the Yii framework, the controller calls the facade method, the facade calls the adapter method, the adapter calls the api method, the api encapsulates the sql method, but in most cases Next, it is just a simple call, but limited to the rules of the current project, methods must be written, and the methods are all simple returns, so I wrote a demo and simulated it.
<?php class aApi { public static function tt1($name, $age) { print_r($name); echo $age; } } class aAdapter { public function call($func, $params) { $class = substr(get_called_class(), 0, -7) . 'Api'; return call_user_func_array(array($class, $func), $params); } } class aFacade { public static function callstatic($func, $params) { // 这里也可以用debug_backtrace() $class = substr(get_called_class(), 0, -6) . 'Adapter'; $obj = new $class(); return call_user_func_array(array($obj, $func), $params); } } class aController { public function actionC() { aFacade::tt1(['name'], 'age'); } } $a = new aController; $a->actionC();
The above is the detailed content of PHP example-Yii framework uses magic method to implement cross-file calling function example. For more information, please follow other related articles on the PHP Chinese website!