Can I use private instance methods as callbacks?

后端 未结 3 490
逝去的感伤
逝去的感伤 2021-01-17 11:18

My particular scenario involves doing some text transformation using regular expressions within a private method. The private method calls preg_replace_callback, but is seem

3条回答
  •  不知归路
    2021-01-17 11:33

    Yes, it seems you can:

    test();
    
    
    //this doesn't work
    $result = preg_replace_callback('/test/', array(new a(), 'replaceCallback'), ' test test ');    
    echo $result;
    

    So it seems that preg_replace_callback(), or PHP's callback mechanism, is aware of the scope in which it was called.

    Tested on 5.2.8

提交回复
热议问题