- microtime: documentation ( source)
- array_merge: documentation ( source)
<?php
class GrandPa {
protected $elements = array(
'a' => 1,
'b' => 2,
);
public function mergeElements($childElements = null)
{
// 用反射实现
$elements = $this->elements;
$class = new ReflectionClass($this);
while($class = $class->getParentClass()) {
$properties = $class->getDefaultProperties();
if(isset($properties['elements'])) {
$elements = array_merge($properties['elements'], $elements);
}
}
return $elements;
}
}
class Father extends GrandPa {
protected $elements = array(
'c' => 3,
);
}
class Me extends Father{
protected $elements = array(
'd' => 4,
);
}
$stime=microtime(true);
for ($i=1;$i<=100000;$i++){
$me = new Me();
$me->mergeElements();
}
$etime=microtime(true);
echo $etime-$stime;