-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
Deprecate Undefined Constant Usage in php.ini Files
The php.ini file parser (as used by parse_ini_file()
) supports constant resolution, including core predefined constants. However, the current behavior with undefined constants is inconsistent with PHP 8's strict handling of undefined constants in regular PHP code.
Problem Statement
Current Inconsistent Behavior
In PHP 8 scripts, undefined constants trigger immediate fatal errors:
<?php
$var = X_ERROR; // Fatal error: Undefined constant "X_ERROR"
However, in php.ini files, undefined constants are silently handled using PHP 4-era semantics:
[PHP]
error_reporting = X_ERROR|E_WARNING|E_PARSE|E_NOTICE
This silently:
- Converts
X_ERROR
to string"X_ERROR"
- Converts that string to integer
0
in bitwise context - Results in value
14
(0 | 4 | 2 | 8) without any indication of the error
This creates invisible configuration errors that are difficult to debug.
Proposed Change
Proposal: Add Diagnostics for Undefined Constants
We propose emitting diagnostics when undefined constants are encountered in configuration files:
During startup (php.ini parsing):
PHP: Use of undefined constant NAME_OF_CONSTANT - assumed 'NAME_OF_CONSTANT' (this will throw an Error in a future version of PHP)
During runtime (parse_ini_file/string):
PHP Warning: Use of undefined constant NAME_OF_CONSTANT - assumed 'NAME_OF_CONSTANT' (this will throw an Error in a future version of PHP) in filename on line X
This makes configuration errors visible and paves the way for future strict handling.
Message Severity Discussion
The proposed warning message and severity mirror the PHP 7.2 approach for undefined constants in scripts:
Warning: Use of undefined constant UNDEFINED_CONSTANT - assumed 'UNDEFINED_CONSTANT' (this will throw an Error in a future version of PHP)
Alternative severity levels could be considered:
Severity | Wording | Intent |
---|---|---|
E_DEPRECATED | "may throw" | Softer transition |
E_WARNING | "will throw" | Clear future intent |
E_NOTICE | "will throw" | Less intrusive |
The PHP 7.2 precedent suggests E_WARNING is appropriate for this level of issue.
Benefits
- Early detection: Configuration errors become visible immediately
- Consistency: Aligns ini parsing behavior with PHP 8 language semantics
- Fail-fast: Helps users identify and fix configuration mistakes sooner
- Future-proof: Enables eventual strict mode matching PHP script behavior
Implementation Notes
The change would be implemented in zend_ini_get_constant()
by adding diagnostics when constant resolution fails. The current ini_error()
function uses ZEND_COLD
(compiler hint for infrequently executed code), which remains appropriate since undefined constants should be rare in production configurations.
This infrastructure would also support a future transition to throwing Errors, though the error handling mechanism might need adjustment at that time.
Note: This section is preliminary and subject to implementation details.
References
Discussion
PHP Manual Pages
3v4l.org PHP Codes
- https://3v4l.org/7PA3u Undefined constant
- https://3v4l.org/vC6mg Unsupported operand types