PHP core functions rewritten to throw exceptions instead of returning false on errors.
Safe-PHP is a library that redeclares core PHP functions to throw exceptions instead of returning false on errors. It solves the problem of silent failures in PHP's error-prone functions by enforcing exception-based error handling, making code more robust and readable.
PHP developers working on applications where reliable error handling is critical, such as those maintaining legacy codebases or building new systems that require strict error management.
Developers choose Safe-PHP because it provides a drop-in replacement for PHP core functions with minimal code changes, integrates seamlessly with PHPStan for automated detection of unsafe usage, and offers automated refactoring tools like Rector for migrating large codebases efficiently.
All PHP functions, rewritten to throw exceptions instead of returning false
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Replaces error-prone false returns with exceptions, ensuring errors are never overlooked, as shown in the README's file_get_contents and json_decode examples.
Includes a PHPStan rule to automatically detect unsafe function usage and suggest Safe replacements, reducing manual oversight in code reviews.
Provides Rector configuration for bulk migration of legacy codebases, easing the transition with a simple command-line tool, as detailed in the installation section.
Covers all PHP core functions that can return false on error, plus safe DateTime and DateTimeImmutable classes, offering broad protection against silent failures.
Adds ~700µs per request by loading 1000+ functions from ~85 files, as acknowledged in the performance impact section, which can be significant for high-traffic applications.
The Rector tool only performs function name replacements without handling existing error checks, requiring manual intervention for complex logic, as warned in the automated refactoring notes.
Requires explicit 'use function' statements for each Safe function, which can lead to verbose imports and is easy to forget without PHPStan enforcement.
Converting functions to throw exceptions can break code that depends on false returns for flow control, necessitating careful testing and refactoring in legacy systems.