Is it possible in PHP to prevent “Fatal error: Call to undefined function”?

后端 未结 6 1952
鱼传尺愫
鱼传尺愫 2020-12-03 17:31

In PHP, is there any way that I can ignore functions that are undefined instead of throwing a fatal error that is visible in the browser?—i.e., Fatal error: Call to

相关标签:
6条回答
  • 2020-12-03 17:57

    What you are asking for seems a little goofy, but you can get a similar effect by declaring all your functions as methods of a class and then implement __call as a method of that class to handle any undefined method calls. You can then handle calls to undefined methods however you like. Check out the documentation here.

    0 讨论(0)
  • 2020-12-03 17:59

    In php 7, this is now possible.

    Example codez:

    try {
        some_undefined_function_or_method();
    } catch (\Error $ex) { // Error is the base class for all internal PHP error exceptions.
        var_dump($ex);
    }
    

    demo

    http://php.net/manual/en/migration70.incompatible.php

    Many fatal and recoverable fatal errors have been converted to exceptions in PHP 7. These error exceptions inherit from the Error class, which itself implements the Throwable interface (the new base interface all exceptions inherit).

    0 讨论(0)
  • 2020-12-03 18:00

    No. Fatal errors are fatal. Even if you were to write your own error handler or use the @ error suppression operator, E_FATAL errors will still cause the script to halt execution.

    The only way to handle this is to use function_exists() (and possibly is_callable() for good measure) as in your example above.

    It's always a better idea to code defensively around a potential (probable?) error than it is to just let the error happen and deal with it later anyway.

    EDIT - php7 has changed this behavior, and undefined functions/methods are catchable exceptions.

    0 讨论(0)
  • 2020-12-03 18:01

    Prevent no. But catch and log yes, using register_shutdown_function()

    See PHP manual

    function shutDown_handler()
    {
       $last_error = error_get_last();
       //verify if shutwown is caused by an error
       if (isset ($last_error['type']) && $last_error['type'] == E_ERROR)
       {
          /*
            my activity for log or messaging
            you can use info: 
              $last_error['type'], $last_error['message'],
              $last_error['file'], $last_error['line']
              see about on the manual PHP at error_get_last()
          */
       }
    }
    
    register_shutdown_function ('shutDown_handler');
    
    0 讨论(0)
  • 2020-12-03 18:09

    we can hide errors but this will log in apache error log

    //Set display error true.

    ini_set('display_errors', "0");
    

    //Report all error except notice

    ini_set('error_reporting', E_ALL ^ E_NOTICE ^ E_STRICT);
    

    //We can use try catch method

    try {
        my_method();
    } catch (\Error $ex) { // Error is the base class for all internal PHP error exceptions.
        var_dump($ex);
    }
    

    //Check method existance

    function_exists()

    0 讨论(0)
  • 2020-12-03 18:13

    If you would like to suppress this error while working with objects use this function:

    function OM($object, $method_to_run, $param){ //Object method
        if(method_exists(get_class($object), $method_to_run)){
            $object->$method_to_run($param);
        }        
    }
    

    Regards

    0 讨论(0)
提交回复
热议问题