- microtime: documentation ( source)
<?php
class ParentClass {
private $property = 'parent private property';
}
class C extends ParentClass {
private $property = 'child private property';
}
$object = new C();
// Using ReflectionProperty
$refProperty = new ReflectionProperty(ParentClass::class, 'property');
$refProperty->setAccessible(true);
$startReflection = microtime(true);
$reflectionValue = $refProperty->getValue($object);
$endReflection = microtime(true);
// Using array casting
$startArray = microtime(true);
$arrayValue = (array)$object;
$arrayValueDirect = $arrayValue["\0" . ParentClass::class . "\0" . 'property'];
$endArray = microtime(true);
echo "Reflection Value: $reflectionValue\n";
echo "Array Casting Value: $arrayValueDirect\n";
echo "Reflection Time: " . ($endReflection - $startReflection) . " seconds\n";
echo "Array Casting Time: " . ($endArray - $startArray) . " seconds\n";
// Using ReflectionProperty
$refProperty = new ReflectionProperty(ParentClass::class, 'property');
$refProperty->setAccessible(true);
$startReflection = microtime(true);
$reflectionValue = $refProperty->getValue($object);
$endReflection = microtime(true);
// Using array casting
$startArray = microtime(true);
$arrayValue = (array)$object;
$arrayValueDirect = $arrayValue["\0" . ParentClass::class . "\0" . 'property'];
$endArray = microtime(true);
echo "Reflection Value: $reflectionValue\n";
echo "Array Casting Value: $arrayValueDirect\n";
echo "Reflection Time: " . ($endReflection - $startReflection) . " seconds\n";
echo "Array Casting Time: " . ($endArray - $startArray) . " seconds\n";