2

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.">";
    }
Stephan
  • 327
  • 3
  • 11

2 Answers2

7

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);
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Jeah I thought so, too. I used the reference for "security" due to php often does strange stuff – Stephan Oct 25 '11 at 19:29
  • @zcei: If you have the feeling that PHP does strange stuff, always double check the manual and ensure you're doing everything right instead of guessing. Otherwise PHP will indeed do strange stuff. – hakre Oct 25 '11 at 19:30
  • hmh, I did, but did not find much about preg_replace_callback with classes. In the end it was due to the reference ampersand... Never trust sites found with google :> – Stephan Oct 25 '11 at 19:35
  • With local vars like $obj as you mentioned it works. But why can't I use a member var $this->obj ? I always get that there is no callback. – Stephan Oct 26 '11 at 14:26
  • @zcei: That works as well, however, ensure that the propery (class variable) already is a callback when you pass it to `preg_replace_callback`. I'll update the answer based on the code fragment of your question. – hakre Oct 26 '11 at 15:21
  • Hm so easy I could have guessed myself. – Stephan Oct 26 '11 at 18:13
  • @zcei: Everything seems easy if you know how ;) – hakre Oct 26 '11 at 22:50
1

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

jchook
  • 6,690
  • 5
  • 38
  • 40