Home >Backend Development >PHP Tutorial >preg_replace_callback 如何回调一个实例方法啊

preg_replace_callback 如何回调一个实例方法啊

WBOY
WBOYOriginal
2016-06-06 20:52:07896browse

mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )

这个函数的$callback,传字符串进去就可以调用,静态方法什么的。那么怎么能指定成一个实例方法呢?

回复内容:

mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )

这个函数的$callback,传字符串进去就可以调用,静态方法什么的。那么怎么能指定成一个实例方法呢?

完全可以啊,使用数组传递就行

class A
{
    public function test($matches)
    {}
}

$a = new A();
preg_replace_callback("/(w+)/", array($a, 'test'), 'asfdasdf asdfsad');
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn