CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Adding dynamic properties by Registrars will be deprecated on PHP 8.2

Open kenjis opened this issue 2 years ago • 9 comments

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

kenjis avatar Jun 20 '22 02:06 kenjis

#[AllowDynamicProperties]

iRedds avatar Jun 20 '22 02:06 iRedds

The issue is we will continue to use Dynamic Properties that PHP will denies by default or not.

kenjis avatar Jun 20 '22 02:06 kenjis

Why not use the Entity::$attributes case?

iRedds avatar Jun 20 '22 04:06 iRedds

Why not use the Entity::$attributes case?

What do you mean? Do you mean that we should change it to use magic properties?

kenjis avatar Jun 22 '22 22:06 kenjis

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?

kenjis avatar Jun 22 '22 22:06 kenjis

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 avatar Jun 23 '22 02:06 iRedds

@iRedds Okay, thanks for explanation. It is an option.

It seems there are three options.

  1. Use #[AllowDynamicProperties]
  2. Use magic properties
  3. 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

kenjis avatar Jun 23 '22 04:06 kenjis

See https://github.com/codeigniter4/CodeIgniter4/pull/6172#issuecomment-1165468080

kenjis avatar Jun 27 '22 08:06 kenjis

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.

iRedds avatar Jun 27 '22 22:06 iRedds

We do not recommend the use of dynamic properties in Registrar.

kenjis avatar Oct 12 '22 00:10 kenjis