Geoff L
Geoff L

Reputation: 805

PHP Uncaught Exception, should be caught

I'm handling some PHP errors during an Ajax call and I get the following message in Chrome Console:

An uncaught Exception was encountered
Type: Error

Message: Call to undefined function data()

Filename: /var/www/application/models/M_ajax.php

Line Number: 604

Backtrace:

File: /var/www/application/controllers/Global_fxns.php
Line: 47
Function: _4___signIn

File: /var/www/html/index.php
Line: 349
Function: require_once

The thing is, its inside a try/catch block. And PHP errors should be caught. For example, if i execute this code inside the try block

strlen();

The code is caught as you would expect, as strlen expects a parameter. But this error is not caught. The line of code that is triggering the error is

if ( ! $this->M_account->addUserTrafficEvent( $userId, data('Y-m-d >H:i:s'), 2.1, '' ) ) {

And it's an error because I meant to type 'date' instead of 'data' to use PHP's date function, and because 'data' is not a function its creating an error. Why isn't this error being caught? Is there some kind of difference between errors created with PHP functions and user-defined functions, and if so shouldn't it still be caught? It's generating a PHP error message after-all

Thanks

Upvotes: 3

Views: 2383

Answers (1)

Stefano Coletta
Stefano Coletta

Reputation: 730

From PHP 7 you can use catch(Error $err).

<?php
try {
    data('Y-m-d >H:i:s');
} catch (Exception $exc) {
    echo 'Fatal exception caught: '.$exc->getMessage();
} catch (Error $err) {
    echo 'Fatal error caught: '.$err->getMessage();
}
?>

Checkout the Fiddle I've made for you

Upvotes: 4

Related Questions