Using magic methods to implement cross file calls

Source: Internet
Author: User


Currently the project uses the YII framework, controller calls the façade method, the façade calls the adapter method, the adapter invokes the API method, the API encapsulates the SQL method, but in most cases it's just a simple call, but limited to the current project's rules, All have to write methods, and the method is simple return, so wrote a demo, simulated the next.


<?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)
    {
        //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 ();


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.