views:

34

answers:

2

First of all, this question is purely theoretical. Fact is, whether it's possible or not, it would be terribly bad practice. Having said that, here's my question:

PHP offers the possibility to define custom error handlers via the set_error_handler function. By parsing the error message, it's possible to find out what triggered the error.

I'm interested mostly in 'Call to undefined function' errors. I know its possible to parse the error message to uncover the called function, and this got me thinking.

Would it be possible for the error handler, in case of an Undefined Function, to attempt to include a file (say functions.html.php for all functions starting with a html_ prefix), and then attempt to re-execute the function, plus arguments, that initially triggered the error? And, most importantly, return the function's value in case of success?

+1  A: 

It's not really possible to restart the execution where the error occured.

However: there is a system to loading classes on demand, using the __autoload function.

From the manual:-

You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

There is more in the PHP manual here: http://php.net/manual/en/language.oop5.autoload.php

rikh
I know about autoloading classes, thanks. What I was looking for, is a way to simulate this behaviour, but for functions instead of classes. (Why? Just to see if it could be done)
Duroth
+1  A: 

In brief without using exception handling you won't be able to recover from the error in the way you described.

There is a way to handle this specifically for undefined functions, however that is to say undefined member functions from an object. This is the __call() method. Basically if you call an undefined method from an object __call() then takes that function call and does whatever you put in the method body see http://php.net/manual/en/language.oop5.overloading.phplink text

RMcLeod