php __call 和 __callStatic 使用

<?php

class Test
{

    protected function haha( $id, $name )
    {
        var_dump( $id, $name );
    }

    public function __call( $name, $params )
    {
        if( ! method_exists($this,$name) ){
            throw new Exception("Call to undefined method " . get_called_class() . '->' . $name);
        }
        return $this->$name( ...$params );
    }

    public static function __callStatic( $name, $params )
    {
        $object = new static;

        if( ! method_exists($object,$name) ){
            throw new Exception("Call to undefined method " . get_called_class() .'->'. $name);
        }
        return $object->$name( ...$params );
    }

}

( new Test )->heihei( 7, 'xiao' );
Test::heihei( 7, 'xiao' );

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章