0

I am finding myself in a bit of a problem. I have two anonymous functions and one calls the other. But when the function $fCompleteDate is called within the function $fFindAndCreateDate

I get the fatal error of:

Function name must be a string.

The function $fFindAndCreateDate is called later on in the script.

I have literally no idea what the problem is.

$fCompleteDate = function($sDate)
{
  $sDate = str_replace('/', '-', $sDate);
  return str_repeat('01-', max(0, 2 - substr_count($sDate, '-'))) . $sDate;
};

$fFindAndCreateDate = function($aStruct)
{
  $aDateStructNames = array('', 'Remark', 'Formatted', 'Extra');
  foreach($aDateStructNames as $sDateStructName)
  {
    echo $fCompleteDate('2001');
    echo  'a : '. $fCompleteDate($aStruct['startDate'.$sDateStructName]);
    echo  'b : '. $fCompleteDate($aStruct['endDate'.$sDateStructName]);
  }
};
2
  • 1
    This is not valid PHP function. JS function and PHP function both are different. Commented Oct 19, 2016 at 9:17
  • I dont know why PHP called this way as Anonymous while nothing is anonymous in this kind of declaration. Commented Oct 19, 2016 at 9:34

1 Answer 1

2

I see that you use Anonymous functions so I updated my answer.

You need to specify that the second function use the first one like this:

$fFindAndCreateDate = function($aStruct) use ($fCompleteDate) {
 ....
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why is the function $fFindAndCreateDate called correctly then?
@KevinKamer Sorry for that, I updated my answer for Anonymous functions. You only need to add use ($fCompleteDate) to second function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.