CrimpJiggler Posted December 22, 2013 Share Posted December 22, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/284893-how-to-use-a-variable-function-as-a-callback/ Share on other sites More sharing options...
Barand Posted December 22, 2013 Share Posted December 22, 2013 perhaps function X($matches) { // SOME CODE } $mo_diagram = 'X'; $bbcode = preg_replace_callback($pattern,$mo_diagram,$bbcode); Quote Link to comment https://forums.phpfreaks.com/topic/284893-how-to-use-a-variable-function-as-a-callback/#findComment-1462942 Share on other sites More sharing options...
Ch0cu3r Posted December 22, 2013 Share Posted December 22, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/284893-how-to-use-a-variable-function-as-a-callback/#findComment-1462943 Share on other sites More sharing options...
kicken Posted December 22, 2013 Share Posted December 22, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/284893-how-to-use-a-variable-function-as-a-callback/#findComment-1462966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.