I have re write my code from old php 7.4 scripts. And i see my code be more slower than old version
My old models export data from method toArray
And his representation was like that
public function toArray()
{
return [
"id" => $this->id;
"name" => $this->name;
//And many other values
]
}
But now, with PHP 8, i have use reflection object to serialize my object. That take something like :
class MyModel extends Model
{
#[AttributeExport]
private int $id;
#[AttributeExport]
private string $name;
}
//In Model
abstract Model implement JsonSerialize
{
public function jsonSerialize(): array
{
$data = [];
$reflection = new \ReflectionClass(get_called_class());
$elements = array_merge($reflection->getProperties(), $reflection->getMethods());
foreach ($elements as $element) {
$exportableAttributes = $element->getAttributes(
AttributeExport::class,
\ReflectionAttribute::IS_INSTANCEOF
);
foreach ($exportableAttributes as $exportableAttribute) {
/** @var AttributeExport $exportableInstance */
$exportableInstance = $exportableAttribute->newInstance();
$exportName = $exportableInstance->hasName() ? $exportableInstance->getName() : $element->getName();
$method = $element->getName();
if ($element instanceof \ReflectionProperty) {
$method = "get" . ucfirst($method);
}
$data[$exportName] = $this->{$method}();
}
}
return $data;
}
}
I may be mistakenly thinking that the problem may come from there. But do you think that on a large volume of data, this strategy can have an impact?
I used more abstract class compared to the old version to avoid code duplication. Dont know if that can impact too