0

Is there any alternative for the __call() magic method in procedural PHP? I want to provide a fallback for undefined functions.

My usage case specifically is that I'm using a famous CMS built in procedural, and I want to use regular functions on the theme files and call those functions from Classes from behind the scenes, so I need some kind of fallback for undefined functions.

Lucas Bustamante
  • 15,821
  • 7
  • 92
  • 86
  • Register an error handler and deal with it there. http://php.net/manual/en/function.set-error-handler.php or test it ahead of time with `function_exists()` – ArtisticPhoenix Apr 08 '18 at 04:43

2 Answers2

0

If you’re using PHP 7, you could wrap your calls to undefined functions in a try {…} catch (Error $e) {…} block and handle the fallback from there. [1]

Pre-PHP7, I can’t think of something that doesn’t involve wrapping all function calls or using function_exists.

Alex
  • 86
  • 1
  • 4
0

I guess what you were searching for might be: $functionName() or call_user_func($functionName) see: How to call PHP function from string stored in a Variable (So all credit goes to: user knittl

(Just saw your question when googling for something similar. So I thought I put the answer here for everyone who searches for the same thing).

J.Doe
  • 1
  • 1