yllen / reports

Additional reports
GNU Affero General Public License v3.0
6 stars 11 forks source link

Using Dynamic Properties on Classes running PHP 8.2 will lead to PHP Deprecated: Creation of dynamic property is deprecated E_DEPRECATED warnings. #30

Open Russell-Jones-OxPhys opened 4 months ago

Russell-Jones-OxPhys commented 4 months ago

In the GLPI reports plugin this shows up as PHP Deprecated function (8192): Creation of dynamic property PluginReportsAutoReport::$plug is deprecated in /var/www/html/glpi/plugins/reports/inc/autoreport.class.php at line 57

https://github.com/yllen/reports/blob/79bf1d98cfb4aadf1a80f31e3cf87c0473fc5eb2/inc/autoreport.class.php#L57

See https://stackoverflow.com/questions/74991682/php-8-2-dynamic-properties-deprecated-how-to-use-them-anyway-in-a-compatible-wa for possible solutions. The options are to rework the class to not use dynamic properties, to extend stdClass, or to add an attribute.

Support will be dropped in PHP9, apparently, but that's not on the PHP version support grid yet https://www.php.net/supported-versions.php

I'd like to submit a PR, but I don't think I'm likely to do that any time soon, as I don't know the plugin code (or GLPI, for that matter) well enough.

AlexMaita commented 2 months ago

Hi @Russell-Jones-OxPhys,

I encountered the same deprecated warning with PHP 8.2 regarding the dynamic property creation in the GLPI reports plugin. The issue occurs because the creation of dynamic properties is deprecated in PHP 8.2 and will be removed in future versions.

To resolve this, we need to declare the properties explicitly within the class. Here's the solution that worked for us:

In the file reports/inc/autoreport.class.php, you need to declare the $plug property explicitly. Below is the modified code:

class PluginReportsAutoReport { private $plug; // Declare the property explicitly

private $criterias = []; private $columns = []; private $group_by = []; private $columns_mapping = []; private $sql = ""; private $name = ""; private $subname = ""; private $cpt = 0; private $title = '';

function __construct($title='') { preg_match('@/(plugins|marketplace)/(.)/report/(.)/@', $_SERVER['SCRIPT_NAME'], $regs); $this->plug = $regs[2]; $this->name = $regs[3]; includeLocales($this->name, $this->plug); $this->setTitle($title); }

// Other class methods }

By explicitly declaring the $plug property as a private property of the class, we eliminate the deprecated warning and adhere to the best practices recommended for future PHP versions.

I hope this helps others facing the same issue. If you need any further assistance or clarification, feel free to ask!

Best regards, Alex Sandro Maita Junior