This article mainly introduced the YII framework uses the Magic method to realize the cross-file invocation function, involving in the YII framework PHP object-oriented programming related operations skills, the need for friends can refer to the following
The example in this paper describes the YII framework using the Magic method to implement cross-file invocation. Share to everyone for your reference, as follows:
Currently project with YII Framework, controller calls facade method, facade call adapter method, adapter Call API method, API encapsulates the SQL method, but most of the time, just simple invocation, but limited to the current project rules, All to write methods, and the method is simply return, so wrote a demo, simulated the next.
<?phpclass 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) { //can also be used here 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 whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!