Using preg_replace_callback with external class

前端 未结 2 1571
情书的邮戳
情书的邮戳 2021-01-15 16:15

I have a question for you!

Normally, if you call a callback function within an OOP context you have to use array(&$this, \'callback_function\')

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 16:31

    Say your external class looks like this

    If your callback function makes no reference to $this, you can call it statically like so:

    preg_replace_callback($pattern, 'ExternalClass::callback', $subject);
    

    Otherwise, your method should work in theory.

    preg_replace_callback($pattern, array(new ExternalClass, 'callback'), $subject);
    

    Read more about callbacks

提交回复
热议问题