是否可以使用类的静态私有方法作为回调?
I saw code that contained the following line:
preg_replace_callback($regex, 'TextileParser::replaceAnchor', $text);
where TextileParser::replaceAnchor()
is a private static method.
我看到包含以下行的代码: p>
preg_replace_callback ($ regex,'TextileParser :: replaceAnchor',$ text);
code> pre>
其中 TextileParser :: replaceAnchor() code>是私有静态 方法。 p>
div>
Yes, it is possible.
Just test it yourself:
<?php
class TestCallBack { private static function found_number($num) { return "-".$num[0]."-"; } public function find($Str) { return preg_replace_callback('/[0-9]/', 'TestCallBack::found_number', $Str); } } // Exemple $Tester = new TestCallBack; $Result = $Tester->find("54321"); var_dump($Result);
on php's website, there's this example:
http://www.php.net/manual/en/function.preg-replace-callback.php#96899
I'd assume if it's on php.net, then it's good to go.