将 preg_replace_callback 与外部类一起使用

发布于 2024-12-11 18:14:40 字数 1343 浏览 0 评论 0原文

我有个问题想问你!

通常,如果您在 OOP 上下文中调用回调函数,则必须使用 array(&$this, 'callback_function')

这就是我的想法。

但现在我想在外部类中调用回调,因为有很多回调函数。出于结构原因,我想给他们一个自己的课程。

我想:“好吧,创建一个此类的实例并传递它而不是 $this。”

所以我尝试使用 array($cb, 'callback_function')array($this->cb, 'callback_function') 但它不起作用。

我做错了什么?

感谢您的帮助!


编辑:

在我的基本类中,我有:

    function __construct()
    {
        // some other vars here

        $this->cb = new Callback();
    }

并用以下方式调用它:

$newline = preg_replace_callback("/(^#+) (.*)/", array(&$this->cb, 'callback_heading'), $newline);

在我的回调类中,我有:

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.">";
    }

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')

That's what I figured out.

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.

I thought: "Ok, make an instance of this class and pass it instead of $this."

So I tried it with array($cb, 'callback_function') and array($this->cb, 'callback_function') but it won't work.

What am I doing wrong?

Thanks for your help!


Edit:

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.">";
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2024-12-18 18:14:40

先评论一下:

通常,如果您在 OOP 上下文中调用回调函数,则必须使用 array(&$this, 'callback_function')

不,通常(现在)它是 array($this, 'callback_function') - 没有 &

然后,您可以放置​​代表对象的任何变量,而不是 $this

$obj = $this;
$callback = array($obj, 'method');

或者

class That
{
   function method() {...}
}

$obj = new That;
$callback = array($obj, 'method');

这就可以了,请参阅 PHP 手册中的回调伪类型


更类似于您问题的代码片段:

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);

First a comment:

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

No, normally (these days) it's array($this, 'callback_function') - without the &.

Then, instead of $this you can put any variable that's representing an object:

$obj = $this;
$callback = array($obj, 'method');

or

class That
{
   function method() {...}
}

$obj = new That;
$callback = array($obj, 'method');

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);
凉薄对峙 2024-12-18 18:14:40

假设你的外部类看起来像这样

<?php
class ExternalClass {
    function callback() {
        // do something here
    }
}

如果你的回调函数没有引用 $this,你可以像这样静态地调用它:

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

否则,你的方法理论上应该可以工作。

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

了解有关回调的更多信息

Say your external class looks like this

<?php
class ExternalClass {
    function callback() {
        // do something here
    }
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文