I’m having a hard time with PHP 8.2’s dynamic properties deprecation. I’m looking here for a permanent solution that will be compatible with PHP 9.0, so adding the #[AllowDynamicProperties]
attribute is not a solution for me.
Here’s my problem. I used to have, for example, the following class:
class planet {
function __construct($id, $a, $m, $i, $e, $π) {
$this->a = $a;
$this->e = $e;
$this->i = $i;
$this->id = $id;
$this->m = $m;
$this->n = .9856076686 / ($a * sqrt($a));
$this->π = $π;
}
}
Then I would create each planet with the following array:
$pl = [
new planet("Mercury", .38709831, 3.3011e23, 7.004986, .20563, 77.456119),
new planet("Venus", .723332982, 4.8675e24, 3.394662, .006772, 131.563703)
];
and so on.
If I try this now, of course I get the now-(in)famous notification:
Deprecated: Creation of dynamic property planet::$a is deprecated
So my question is the following:
How can I keep creating my planets from an array? It seems to be possible to create single objects with instructions such as
$Mercury = new StdClass(); $Mercury->a = .38709831;
but this would end up in long code for nothing, I find.
Is there a faster, more efficient way?
I googled and googled, but found nothing that even remotely helps.