Skip to content

Throwables (Error & Exception Handling)

Richard T. Miles edited this page Mar 7, 2024 · 1 revision

The example repository used in this document can be found here.

CarbonPHP is free open-source software that ships with a few major features. The first we will cover is a beautiful error reporting system with code sample output of where the issue arose. The second is a MySQL ORM for querying your database. CarbonPHP is a part of the larger CarbonORM organization. We plan to add a websocket demo here soon.

Real time beautiful error reports!

Screenshot 2023-12-26 at 1 30 50 AM

Logs using error_log()

Screenshot 2023-12-26 at 1 21 16 AM

Robust and Verbose

Screenshot 2023-12-26 at 12 47 36 AM Screenshot 2023-12-26 at 12 47 44 AM

Explain how you would handle exceptions in PHP. Provide an example.

There are two types of throwable issues in PHP: Exceptions and Errors. Both Errors and Exceptions extend the Throwable interface. Both types of Throwable issues can be handled globally using the php internal set_exception_handler and set_error_handler functions respectively. What types of errors are thrown can be controlled using the error_reporting function. Some errors are not catchable using Try {} catch () {} blocks, such as E_DEPRECATED and must be handled with global handlers. Other oddities should be noted compile time vs runtime. Implementing a global handlers and try catch blocks should be done.

Let's look at an example of my Throwable Handler. If you do not have Composer installed globally you can use the shell installer also provided in this repo.

chmod +x ./downloadComposer.sh
./downloadComposer.sh

Update the configurations database username and password. The default is root and password. This is located ./examples/Sample.php. You can use this command cat ./examples/Sample.php | grep -n CarbonPHP::DB_USER to find the line number or optionally use the following sed command to update the file. You'll still need to modify the user and password (MySQLUsername & MySQLPassword) from the command below.

sed -i '' -E "s|CarbonPHP::DB_USER => '(.*)',|CarbonPHP::DB_USER => 'MySQLUsername',|g" ./examples/Sample.php
sed -i '' -E "s|CarbonPHP::DB_PASS => '(.*)',|CarbonPHP::DB_PASS => 'MySQLPassword',|g" ./examples/Sample.php

Once you have composer installed you can use it to install the dependencies and set up our PSR-4 namespacing for this project. Many projects incorrectly use a global error handler without unregistering it. This can cause issues with other libraries. Because of this it is common to invoke ThrowableHandler::start(); in multiple places. This is not necessary, but again common to override others poor practices.

./composer.phar install

Now we can run the example using php's built in web server. Note PHP's server does have a small memory leak, so it is not recommended for production or large projects.

php -S 0.0.0.0:8000 error.php

Then visit http://localhost:8000/ in your browser. Some OS' support:

open http://localhost:8000/

This script invokes the global error handler and throws an exception. The exception is caught and the error is displayed. For a more fine-grained control over the error handling you can use the try {} catch () {} blocks without starting the global handler.

try {

    throw new Exception('This is a test');

} catch (Throwable $e) {

    ThrowableHandler::generateLog($e);
}

Write a PHP script using PDO to connect to a MySQL database and execute a SELECT query.

The following command will start a php server on port 8000 and serve the connect.php file. This demonstrates a connection to a MySQL database using PDO.

php -S 0.0.0.0:8000 connect.php

SQL injection is easily prevented by using prepared statements. CarbonPHP uses PDO and prepared statements by default and automates the process in a way that is seamless and simple to use. This file is largely responsible for the REST ORM PDO foundation.