class sample {
    function __call($a, $b) {
        echo ucwords(implode(' ', $b).' '.$a);
    }    function ads() {
        ob_start();
        echo 'by';
        return $this;
    }
    function ade() {
        $c = ob_get_clean();
        $this->php('power',$c);
    }}
$inst = new sample();
$inst->cmstop('welcome', 'to');  //Welcome To Cmstopinst->ads()->ade(); //Power By Php

解决方案 »

  1.   

    __call的用法说句简单就是当你调用了一个类中不存在的方法,它就会自动去调用__call。ruby中有个method_missing的方法叫幽灵方法,也是这个意思。call的应用场合:
    class test{    const DE = '_';
        private $value;    public function __call( $funcname, $arguments ){
            
            switch( $funcname ){
                case 't':
                   $this->value = 123;
                   break;
                case 'x':
                   $this->value = 456;
                   break;
    default :
                   $this->value = 789;
                   break;
            }
            if( method_exists( $this, self::DE . $funcname) ){
               call_user_func( array($this, self::DE . $funcname) );
            }
            else{
               echo '"' , $funcname , '"' , ' is not a method!<br/>';
            }    }    private function _t(){
            echo $this->value , "<br/>";
        }    private function _x(){
            echo $this->value , "<br/>";
        }
    }$test = new test();
    $test->t();
    $test->x();
    $test->a();
      

  2.   

    <?php
    class sample {
        function __call($a, $b) {
    // 解释一下
    // __call的第一个参数$a, 是不存在的方法的名,在这里就是cmstop
    // __call的第二个参数$b, 是调用方法的参数列表, 在这里就是array('welcome', 'to');
    // 不要将 $a、$b 和 welcome、to 对应 
            echo ucwords(implode(' ', $b).' '.$a);
        }    function ads() {
            ob_start();
            echo 'by';
            return $this;
        }
        function ade() {
            $c = ob_get_clean();
            $this->php('power',$c);
        }}
    $inst = new sample();
    $inst->cmstop('welcome', 'to');  //Welcome To Cmstopinst->ads()->ade(); //Power By Php