Work in progress
A set of core PHP functions rewritten to throw exceptions instead of returning false when an error is encountered.
Most PHP core functions have been written before exception handling was added to the language. Therefore, most PHP functions
do not throw exceptions. Instead, they return false in case of error.
But most of us are too lazy to check explicitly for every single return of every core PHP function.
// This code is incorrect. Twice.
// "file_get_contents" can return false if the file does not exists
// "json_decode" can return false if the file content is not valid JSON
$content = file_get_contents('foobar.json');
$foobar = json_decode($content);The correct version of this code would be:
$content = file_get_contents('foobar.json');
if ($content === false) {
throw new FileLoadingException('Could not load file foobar.json');
}
$foobar = json_decode($content);
if ($foobar === false) {
throw new FileLoadingException('foobar.json does not contain valid JSON: '.json_last_error());
}Obviously, while this snippet is correct, it is less easy to read.
Enters thecodingmachine/safe aka Safe-PHP.
Safe-PHP redeclares all core PHP functions. The new PHP functions are acting exactly as the old ones, except they are
throwing exceptions properly when an error is encountered. The "safe" functions have the same name as the core PHP
functions, except they are in the Safe namespace.
use Safe\file_get_contents;
use Safe\json_decode;
// This code is both safe and simple!
$content = file_get_contents('foobar.json');
$foobar = json_decode($content);Use composer to install Safe-PHP:
$ composer require thecodingmachine/safe- handle objects methods overloading
- develop a PHPStan extension
The lib.php file that contains all the functions is auto-generated from the PHP doc.
Read the CONTRIBUTING.md file to learn how to regenerate it and to contribute to this library.