开发者

On error, attempt to include a file and re-execute the failed function

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 m开发者_开发百科essage, 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜