CodeIgniter Dynamic Property Deprecation Error in PHP 8.2
Problem Statement
When running CodeIgniter 3 applications on PHP 8.2+, you may encounter this error:
Severity: 8192
Message: Creation of dynamic property CI_URI::$config is deprecated
Filename: core/URI.php
Line Number: 102
This warning occurs in the CodeIgniter core files, typically in classes like:
CI_URI
CI_Router
CI_Loader
CI_Controller
CI_DB_driver
The key issue is that PHP 8.2 introduced a deprecation warning for dynamic property creation - when classes reference properties that haven't been declared in the class definition.
Why This Happens
CodeIgniter 3 relies heavily on dynamic properties where class properties are created at runtime without explicit declaration. PHP 8.2 deprecated this pattern:
// In CodeIgniter's CI_URI class:
$this->config =& $config; // $config property not declared
This produces E_DEPRECATED
warnings (error level 8192) that can fill your logs and disrupt application behavior.
Important
This is not a fatal error but should be addressed:
- Deprecation warnings indicate future PHP compatibility issues
- Log files can become bloated with repeated warnings
- May cause visual disruption in development environments
Recommended Solution: Add AllowDynamicProperties Attribute
The most maintainable solution is to add the #[\AllowDynamicProperties]
attribute to affected CodeIgniter core classes:
Steps to Apply the Fix
Open these core files:
/system/core/URI.php
/system/core/Router.php
/system/core/Loader.php
/system/core/Controller.php
/system/database/DB_driver.php
Add the
#[\AllowDynamicProperties]
attribute immediately above each class declaration:
#[\AllowDynamicProperties]
class CI_URI {
#[\AllowDynamicProperties]
class CI_Router {
#[\AllowDynamicProperties]
class CI_Loader {
#[\AllowDynamicProperties]
class CI_Controller {
#[\AllowDynamicProperties]
abstract class CI_DB_driver {
Why This Works
- The
#[\AllowDynamicProperties]
attribute tells PHP to allow dynamic property creation - Minimal code change with maximum compatibility
- Preserves original framework behavior
- Officially recommended by the CodeIgniter community
TIP
After making these changes, clear your browser cache and restart your web server to ensure the changes take effect.
Alternative Solution: Declare Missing Properties
For specific classes triggering warnings, explicitly declare the missing properties:
class CI_URI {
/**
* Reference to the configuration object
* @var CI_Config
*/
public $config;
// ... rest of the class ...
}
When to Use This Approach
- Only for one-off fixes in custom classes
- When you need to quickly resolve a single deprecation warning
- When modifying vendor files temporarily
Limitations
This approach becomes impractical when:
- Dealing with multiple affected classes
- Runtime creates different properties dynamically
- The needed properties aren't easily identifiable
Solutions to Avoid
❌ Suppressing Errors with @ Operator
// Don't do this!
$object = @new CI_URI();
- Masks the underlying problem
- May hide other important warnings
- Considered bad practice
❌ Disabling Deprecation Warnings
; In php.ini - not recommended
error_reporting = E_ALL & ~E_DEPRECATED
<?php
// In code - not recommended
ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
- Suppresses valuable diagnostic information
- Will become ineffective in future PHP versions
- May hide other legitimate issues
Ultimate Solution: Upgrade to CodeIgniter 4
CodeIgniter 4 has full PHP 8.2+ support without deprecation issues:
composer create-project codeigniter4/appstarter
Migration Benefits
- Official PHP 8.2+ compatibility
- Modern framework architecture
- Continued maintenance and security updates
- Similar syntax with added features
Conclusion
For PHP 8.2 and later, add #[\AllowDynamicProperties]
to CodeIgniter 3 core classes as shown to resolve dynamic property deprecation warnings:
- Locate affected classes in
/system/core/
and/system/database/
- Add
#[\AllowDynamicProperties]
above each class declaration - Save changes and restart services
- Verify warnings have disappeared
This fix maintains application functionality while preserving important deprecation warnings for other parts of your codebase. For long-term stability, plan migration to CodeIgniter 4.
Note
Always make core file changes in a controlled environment and maintain backups before modifying framework files.