Something like a callback delegate function in php

前端 未结 3 1581
时光取名叫无心
时光取名叫无心 2020-12-16 13:44

I would like to implement something similar to a c# delegate method in PHP. A quick word to explain what I\'m trying to do overall: I am trying to implement some asynchron

3条回答
  •  悲哀的现实
    2020-12-16 14:00

    How do you feel about using the Observer pattern? If not, you can implement a true callback this way:

    // This function uses a callback function. 
    function doIt($callback) 
    { 
        $data = "this is my data";
        $callback($data); 
    } 
    
    
    // This is a sample callback function for doIt(). 
    function myCallback($data) 
    { 
        print 'Data is: ' .  $data .  "\n"; 
    } 
    
    
    // Call doIt() and pass our sample callback function's name. 
    doIt('myCallback'); 
    

    Displays: Data is: this is my data

提交回复
热议问题