Passing static methods as arguments in PHP

前端 未结 7 1401
温柔的废话
温柔的废话 2021-02-02 07:34

In PHP is it possible to do something like this:

myFunction( MyClass::staticMethod );

so that \'myFunction\' will have a reference to the stati

7条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 08:13

    If you want to avoid strings, you can use this syntax:

    myFunction( function(){ return MyClass::staticMethod(); } );
    

    It is a little verbose, but it has the advantage that it can be statically analysed. In other words, an IDE can easily point out an error in the name of the static function.

提交回复
热议问题