Syntax of Closure in PHPDoc

后端 未结 2 1521
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 03:03

I cant find any documentation on the Closure type in PHPDoc. So my question is how do I define the parameter of the parameters sent to the closure and its return value ?

2条回答
  •  离开以前
    2020-12-31 03:12

    use indirect technique

    Your code:

    /**
     * @param MyCustomClass  $cls
     * @param MyFancyClosure $callback
     *
     * @return MyOtherCustomClass
     */
    function changer($cls, $callback){
        return $callback($cls, 2, "a string");
    }
    
    changer($aCustomeClass, function($cls, $int, $string){
       return new MyOtherCustomClass($cls, $int, $string);
    })
    

    and than provide a dummy code somewhere:

    /**
     * this is awesome closure!
     */
    class MyFancyClosure {
        /**
         * @param MyCustomClass $cls
         * @param int           $int
         * @param string        $str
         *
         * @return MyOtherCustomClass
         */
        public function __invoke($cls, $int, $str) {}
    }
    

    note:

    1. The function body of __invoke is not required, so leave it blank.
    2. Use "Closure" sufix for class name to clarify it.

提交回复
热议问题