CodeIgniter4
CodeIgniter4 copied to clipboard
Adding dynamic properties by Registrars will be deprecated on PHP 8.2
Registrars can add dynamic properties now.
But the sample code in the user guide will cause the error on PHP 8.2. https://codeigniter4.github.io/CodeIgniter4/general/configuration.html#explicit-registrars
bash-3.2$ php public/index.php
[ErrorException]
Creation of dynamic property Config\MySalesConfig::$actual is deprecated
at SYSTEMPATH/Config/BaseConfig.php:211
Backtrace:
1 SYSTEMPATH/Config/BaseConfig.php:211
CodeIgniter\Debug\Exceptions()->errorHandler(8192, 'Creation of dynamic property Config\\MySalesConfig::$actual is deprecated', '.../CodeIgniter4/system/Config/BaseConfig.php', 211)
2 SYSTEMPATH/Config/BaseConfig.php:64
CodeIgniter\Config\BaseConfig()->registerProperties()
3 APPPATH/Controllers/Home.php:9
CodeIgniter\Config\BaseConfig()->__construct()
4 SYSTEMPATH/CodeIgniter.php:896
App\Controllers\Home()->index()
5 SYSTEMPATH/CodeIgniter.php:466
CodeIgniter\CodeIgniter()->runController(Object(App\Controllers\Home))
6 SYSTEMPATH/CodeIgniter.php:349
CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)
7 FCPATH/index.php:55
CodeIgniter\CodeIgniter()->run()
bash-3.2$ php -v
PHP 8.2.0-dev (cli) (built: Jun 20 2022 00:32:41) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0-dev, Copyright (c) Zend Technologies
with Zend OPcache v8.2.0-dev, Copyright (c), by Zend Technologies
References:
- https://wiki.php.net/rfc/deprecate_dynamic_properties
- #6160
#[AllowDynamicProperties]
The issue is we will continue to use Dynamic Properties that PHP will denies by default or not.
Why not use the Entity::$attributes case?
Why not use the Entity::$attributes case?
What do you mean? Do you mean that we should change it to use magic properties?
I don't know the needs to add properties dynamically to Config objects. I think dynamic properties are bad practice. So it will be rejected by default in PHP in the future.
We can override config properties by Registrars now. What's missing?
What do you mean? Do you mean that we should change it to use magic properties?
That is, all properties not previously defined should be placed in a separate container, which will have access through magic methods.
I'm just suggesting solutions to the problem in 8.2. This does not mean that I agree with the use of dynamic properties.
@iRedds Okay, thanks for explanation. It is an option.
It seems there are three options.
- Use
#[AllowDynamicProperties]
- Use magic properties
- Do not support dynamic property injection
I think 3. is simple and preferable.
I did not know that Registrars can add dynamic properties. Not documented until recently. See #6160
See https://github.com/codeigniter4/CodeIgniter4/pull/6172#issuecomment-1165468080
I do not know what to say. The 8.2 conflicts you mentioned are mostly due to class design. https://github.com/codeigniter4/CodeIgniter4/blob/053d669157c00af26e72abfe949f138e9803f6a2/system/Database/BaseConnection.php#L336-L340
But the tests/_support/Config/Registrar.php class does not define a new property in the database configuration class.
We do not recommend the use of dynamic properties in Registrar.