Storing a Closure Function in a Class Property in PHP

后端 未结 6 1025
清歌不尽
清歌不尽 2021-01-21 07:37

ok I do have the code below

bar();
       }
    }

    $mee         


        
6条回答
  •  无人及你
    2021-01-21 08:14

    Use call_user_func() function:

    bar);
           }
        }
    
        $mee = new foo();
    
        //save a closure function on the property
        $mee->bar = function(){
            echo 'hahaha';
        };
    
        //invoke the closure function by using a class method
        $mee->boo();
    

    This will display "ahahah"

    Hope it helps.

提交回复
热议问题