1

I'm using an error log function and I want to include the function where the error occurred during the error log. Is there a way to write the function instead of writing the function every time?

<?php 

$a = null;

exampleFunction($a);

function exampleFunction($variable){

  if(is_null($variable)){

    errorLog("variable is null. Function : exampleFunction()");

  }

}

function errorLog($text){

 error_log($text, 3, ".testLog");

}

?>

__FUNCTION__ is not solution. If I use __FUNCTION__ I get "errorLog". I want to know the name of the function that is running errorLog.

For example ;

function errorLog($text){

  error_log($text.' Function : '.$functionName, 3, ".testLog");

}
Yusuf Y.
  • 84
  • 10

2 Answers2

1

Yes, you can use the __FUNCTION__ constant. It's _ _ (2 underscores) "function" _ _ (2 underscores)

function whatever($stuff){
echo "The function name is " . __FUNCTION__;
}

If you want to take it a step further, if it's in a class...

echo "The class name is " . __CLASS__ . " and the function is " . __FUNCTION__;
Nerdi.org
  • 895
  • 6
  • 13
0

For that, you can use the magic constant __FUNCTION__

asiby
  • 3,229
  • 29
  • 32