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

后端 未结 6 1971
鱼传尺愫
鱼传尺愫 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 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.

提交回复
热议问题