How to implement cross-file invocation through the YII framework using the Magic method

Source: Internet
Author: User
Tags yii
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.