Call backs and passing variables?

Hi,

I was looking at some code…

$regExp = new RegExp();
$songText = '99 bottles of beer on the wall.';

$songText = $regExp->replace(
    $string,
    function ($match) {
        return (int)$match - 1;
    }
);
echo $songText;

  // echos 98 bottles of beer on the wall.


class RegExp
{
    public function replace($string, $callback)
    {
        return preg_replace_callback(
            sprintf("/%s/%s", $this->_expr, $this->_flags),
            function ($hit) use ($callback) {
                return call_user_func($callback, $hit[0]);
            },
            $string
        );
    }
}

And I was wondering, where are $hit and $match coming from? I saw the example in the PHP docs for preg_replace_callback. Is $match just something that is a given in this function? I could find nothing about $hit, other than the “use” clause for closures, But, I still don’t get where $hit comes from. There seems to be something fundamental I am missing to understand how this use of a closure works (and to use it similarly in the future).

Any knowledgeable explanation on how this works would be greatly appreciated.

Scott

that’s actually not an issue of Closures, but of callback functions.

When you pass a callback to a function (or method), this function will at one point call this callback (that‘s why it’s called “callback”) and in this call it may pass any parameter to that callback that it desires.

this can be seen explicitly in: call_user_func($callback, $hit[0]) where $hit[0] is passed as the first parameter to the callback function (you could also write it as $callback($hit[0])). What that parameter is named in the callback function depends on who defined the callback. But not only user-created function do that, there are also a couple of native functions that do that (e.g. preg_replace_callback()) and obviously you cannot see how and which parameters are passed there except when you have a look at the Documentation or the PHP source code.

Thanks for that. I am still not completely clear on where the $matches comes from. Is that part of the preg_replace_callback() function? The docs shows this variable in a couple of the examples and says, if a match if found, it is returned, but doesn’t specifically say you can use the $matches variable to get the returned value.

Scott

From the Manual:

callback
A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. This is the callback signature:

string handler ( array $matches )

So the signature defines that the function returns a String (which is then used as the replacement value for a given input), and receives the array $matches as input. $matches is defined as the multi-match array ($matches[0] is the full pattern match, $matches[1] is the first subpattern within that pattern, etc)

Hm, I find the description pretty head on:

callback (that thing we’re talking about)
A callback that will be called and passed an array of matched elements (PHP does this for you) in the subject string (the 3rd parameter in preg_replace_callback()).

Note: comments in parentheses by me

I don’t know how in the world I missed that. I read that section of the PHP manual at least 3 times. :grimacing:

Now it all makes perfect sense. :+1:

Scott

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.