E_ERROR (1) identifies a fatal runtime error. An uncaught throwable can also terminate a script. Some examples below fail during compilation or throw an Error in modern PHP.
A simple example
E_ERROR
Start withTwo declarations of the same class name
Do thisLoad both declarations
ResultFatal error
PHP cannot register both classes under one name, so execution stops.
Example: #1- PHP Error Constant E_ERROR
Two course modules register a helper class with the same name. The class declarations collide when the application loads both modules.
PHP diagnosticE_ERROR
Output excerptFatal error: Cannot declare class ErrorHandlingExample, because the name is already in use in…
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function errorfulMethod1() {
}
}
// The class has a duplicate name
class ErrorHandlingExample {
public function errorfulMethod2() {
}
}
$obj = new ErrorHandlingExample();
$obj->errorfulMethod1();
Output
Fatal error: Cannot declare class ErrorHandlingExample, because the name is already in use in ~\Documents\php-error-constants.php on line 14
Advertisements
Example: #2- PHP Error Constant E_ERROR
A timetable view invokes a helper method removed in a recent edit. If an Error escapes to the top of the request, the page stops rendering.
PHP diagnosticE_ERROR
Output excerptFatal error: Uncaught Error: Call to undefined method ErrorHandlingExample::wrongmethod() in …
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function errorfulMethod() {
}
}
$obj = new ErrorHandlingExample();
$obj->undefinedmethod();
Output
Fatal error: Uncaught Error: Call to undefined method ErrorHandlingExample::wrongmethod() in ~\Documents\php-error-constants.php:14
Stack trace:
#0 {main}
thrown in ~\Documents\php-error-constants.php on line 14
Example: #3- PHP Error Constant E_ERROR
A developer pastes a revised helper into a class and leaves the original method in place. The duplicate declarations prevent PHP from compiling the class.
PHP diagnosticE_ERROR
Output excerptFatal error: Cannot redeclare ErrorHandlingExample::errorfulMethod() in ~\Documents\php-error…
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function errorfulMethod() {
// Implementation code...
}
// The method with same name of an exising method
// It will throw an fatal error
public function errorfulMethod() {
// Implementation code...
}
}
$obj = new ErrorHandlingExample();
$obj->errorfulMethod();
Output
Fatal error: Cannot redeclare ErrorHandlingExample::errorfulMethod() in ~\Documents\php-error-constants.php on line 14
Example: #4- PHP Error Constant E_ERROR
A school help desk receives severity codes from two failed page requests and one warning. Its daily summary counts records classified as E_ERROR so the maintainer can prioritize the failed requests.
A classroom registration form submits fewer arguments than its helper requires. On modern PHP, the call raises ArgumentCountError.
PHP diagnosticE_ERROR
Output excerptFatal error: Uncaught ArgumentCountError: Too few arguments to function ErrorHandlingExample:…
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function printMessage($message) {
echo($message);
}
}
$obj = new ErrorHandlingExample();
$obj->printMessage();
Output
Fatal error: Uncaught ArgumentCountError: Too few arguments to function ErrorHandlingExample::printMessage(), 0 passed in ~\Documents\php-error-constants.php on line 14 and exactly 1 expected in ~\Documents\php-error-constants.php:8
Stack trace:
#0 ~\Documents\php-error-constants.php(7): ErrorHandlingExample->printMessage()
#1 {main}
thrown in ~\Documents\php-error-constants.php on line 8
Example: #6- PHP Error Constant E_ERROR
Two lesson files introduce a helper function with the same name. Loading the second declaration prevents the request from finishing.
PHP diagnosticE_ERROR
Output excerptFatal error: Cannot redeclare errorfulMethod() (previously declared in ~\Documents\php-error-…
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
function errorfulMethod() {
// Implementation code...
}
// The method with same name of an exising method
// It will throw an fatal error
function errorfulMethod() {
// Implementation code...
}
Output
Fatal error: Cannot redeclare errorfulMethod() (previously declared in ~\Documents\php-error-constants.php:8) in ~\Documents\php-error-constants.php on line 14
Advertisements
Example: #7- PHP Error Constant E_ERROR
A reporting page moves to a machine without its configured PDO driver. Opening the database connection raises an exception that the application can record and handle.
PHP diagnosticE_ERROR
First output lineFatal error: Uncaught PDOException: could not find driver in ~\Documents\php-error-constants.php:7
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// Open a PDO connection without enabling a PDO driver
$dbh = new PDO('mysql:host=localhost;dbname=test', "root", "password");
Output
Fatal error: Uncaught PDOException: could not find driver in ~\Documents\php-error-constants.php:7
Stack trace:
#0 ~\Documents\php-error-constants.php(7): PDO->__construct('mysql:host=loca...', 'root', 'password')
#1 {main}
thrown in ~\Documents\php-error-constants.php on line 7