How to call callback method for class?

2019-06-11 09:45发布

问题:

Help me please:

class xam{

       public static function __callStatic($name, $arguments) {
            self::$name();
        }

        static protected function mycallback(){
            echo 'mycallback';
        }
    }

    function doIt($callback) { $callback(); }

I am trying:

doIt(xam::mycallback);

Error:

Fatal error: Undefined class constant 'mycallback'

I know one variant:

doIt(function(){xam::mycallback();});

But it may have an alternative ? Thanks for your help .

回答1:

You can specify the callback as the string "xam::mycallback" or array ['xam', 'mycallback']. The manual describes all the options for specifying callables.



回答2:

Try using this...

doIt('xam::mycallback');


标签: php oop