Jump to content

How to use a variable function as a callback


CrimpJiggler

Recommended Posts

I've just started learning about callbacks and am still trying to get my head around them. So heres what I'm trying to do:

$mo_diagram = function ($matches)
{
// SOME CODE
}
$bbcode = preg_replace_callback($pattern,$mo_diagram,$bbcode);

but it doesn't seem to be working. It works when I add the anonymous function directly into it like this:

$bbcode = preg_replace_callback($pattern,
function ($matches)
{
// SOME CODE
},
$bbcode);

but sometimes its more convenient to put the callback function somewhere else. Also, it works with a non anonymous function like this:

function myCallback ($matches)
{
// SOME CODE
}
$bbcode = preg_replace_callback($pattern,;'myCallback',$bbcode);

but in this case I get an error message saying "cannot redeclare function myCallback" because this particular piece of code is repeated multiple times. A side question: can I solve that error if I were to put this piece of code inside an object, then call a new instance of the object each time I wanna repeat the code?

Link to comment
Share on other sites

function myCallback ($matches)
{
// SOME CODE
}
$bbcode = preg_replace_callback($pattern,;'myCallback',$bbcode);

but in this case I get an error message saying "cannot redeclare function myCallback" because this particular piece of code is repeated multiple times.

You are getting that error because you are defining the myCallback function more than once, eg

// define myCallback for first time
function myCallback ($matches)
{
// SOME CODE
}
$bbcode = preg_replace_callback($pattern1,'myCallback',$bbcode); // callback #1

// define myCallback a second time
function myCallback ($matches)
{
// SOME CODE
}
$bbcode = preg_replace_callback($pattern2,'myCallback',$bbcode); // callback #2

it is ok to have multiple calls to the same callback function

// define myCallback once
function myCallback ($matches)
{
// SOME CODE
}

$bbcode = preg_replace_callback($pattern1,'myCallback',$bbcode); // callback #1
$bbcode = preg_replace_callback($pattern2,'myCallback',$bbcode); // callback #2
Link to comment
Share on other sites

I've just started learning about callbacks and am still trying to get my head around them. So heres what I'm trying to do:

$mo_diagram = function ($matches)
{
// SOME CODE
}
$bbcode = preg_replace_callback($pattern,$mo_diagram,$bbcode);
but it doesn't seem to be working.

 

You need a ; after the function definition. An anonymous function declaration is a statement, and just like any other statement, needs to be terminated by a semi-colon.

 

$mo_diagram = function ($matches)
{
// SOME CODE
};

$bbcode = preg_replace_callback($pattern,$mo_diagram,$bbcode);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.