Fatal Error is the Fatal Error of PHP - How to avoid it

One of the disadvantages of PHP, and one of the things I don't like about the php, is the fatal error, because there isn't any way to avoid it. Well PHP is actually thought to be used in web environment, therefor it is not so important, if your script causes a fatal error, then only the execution of the script for a single request would be terminated.

In PHP command line apps, it is really fatal, if your app causes a fatal error, because you have to restart your app. Well you can avoid this, if you write your app very carefully. You should always use if(function_exists("a_func")) and if(method_exists("a_method")). Now you can be pretty sure, that the script won't cause any fatal error.
If you dynamically load third party modules in your app, you have a real problem, because the included module could cause a fatal error, your app would be terminated. There is on way to avoid it. Use call_user_func. You can replace in third party modules every function call and method call with call_user_func. For example.

<?php
function add($a,$b){
 echo 
$a+$b;
}
// call the function with a small mistake
ad(2,3); // causes a fatal error
//replace it with
call_user_func("ad",2,3); // causes a warning
?>

add new comment

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

Datenschutz | Impressum