对外部类使用preg_replace_callback [英] Using preg_replace_callback with external class
问题描述
我有一个问题要问你!
通常,如果您在OOP上下文中调用回调函数,则必须使用array(&$this, 'callback_function')
Normally, if you call a callback function within an OOP context you have to use array(&$this, 'callback_function')
这就是我想出的.
但是由于需要大量的callback_functions,现在我想在外部类中调用回调.出于结构原因,我想给他们自己的课.
But now I want to call a callback in an external class, due to having to much callback_functions. I want to give them an own class for structure reasons.
我想:好吧,创建一个此类的实例,并传递它而不是$ this."
I thought: "Ok, make an instance of this class and pass it instead of $this."
所以我用array($cb, 'callback_function')
和array($this->cb, 'callback_function')
尝试了一下,但是它不起作用.
So I tried it with array($cb, 'callback_function')
and array($this->cb, 'callback_function')
but it won't work.
我在做什么错了?
感谢您的帮助!
在我的基础课上,我有:
In my basic class I have:
function __construct()
{
// some other vars here
$this->cb = new Callback();
}
并通过以下方式调用它:
And calling it with:
$newline = preg_replace_callback("/(^#+) (.*)/", array(&$this->cb, 'callback_heading'), $newline);
在我的回调类中,我有:
And in my callback class I have:
class Callback
{
function __construct()
{
$this->list = array("num" => 0, "dot" => 0, "normal" => 0);
$this->td = array("strike" => false, "bold" => false, "italic" => false, "underline" => false, "code" => false);
}
public function callback_heading($parameter)
{
$hashs = strlen($parameter[1]);
$hashs++;
if($hashs > 6)
$hashs = 6;
return "<h".$hashs."><span class=\'indented\'>".$parameter[1]."</span><strong>".$parameter[2]."</strong></h".$hashs.">";
}
推荐答案
首先发表评论:
通常,如果您在OOP上下文中调用回调函数,则必须使用
array(&$this, 'callback_function')
不,通常(这些天)是array($this, 'callback_function')
-没有&
.
No, normally (these days) it's array($this, 'callback_function')
- without the &
.
然后,您可以放置表示对象的任何变量来代替$this
:
Then, instead of $this
you can put any variable that's representing an object:
$obj = $this;
$callback = array($obj, 'method');
或
class That
{
function method() {...}
}
$obj = new That;
$callback = array($obj, 'method');
这确实有效,请参见PHP手册中回调伪类型的文档.
This just works, see the documentation of the callback pseudo type in the PHP Manual.
与您的问题的代码片段更相似:
More similar to the code fragments of your question:
class Callback
{
function __construct()
{
$this->list = array("num" => 0, "dot" => 0, "normal" => 0);
$this->td = array("strike" => false, "bold" => false, "italic" => false, "underline" => false, "code" => false);
}
public function callback_heading($parameter)
{
$hashs = min(6, 1+strlen($parameter[1]));
return sprintf("<h%d><span class=\'indented\'>%s</span><strong>%s</strong></h%d>", $hashs, parameter[1], $parameter[2], $hashs);
}
}
class Basic
{
/** @var Callback */
private $cb;
function __construct()
{
// some other vars here
$obj = new Callback();
$this->cb = array($obj, 'callback_heading');
}
function replace($subject)
{
...
$result = preg_replace_callback($pattern, $this->cb, $subject);
}
}
$basic = new Basic;
$string = '123, test.';
$replaced = $basic->replace($string);
这篇关于对外部类使用preg_replace_callback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!