Skip to content

Instantly share code, notes, and snippets.

@arnaud-lb
Forked from pierrejoye/gist:1382376
Created November 21, 2011 11:44
Show Gist options
  • Save arnaud-lb/1382400 to your computer and use it in GitHub Desktop.
Save arnaud-lb/1382400 to your computer and use it in GitHub Desktop.
get_ vs reflection
<?php
class foo {
public $a = 1;
private $b = 2;
public $c = 3;
public $d = 4;
protected $e = 5;
public function methodA() {}
private function methodB() {}
public function methodC() {}
}
$object = new foo;
$iterations = 100;
$s = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$result['propertiess'] = array_flip(array_keys(get_object_vars($object)));
$methods = get_class_methods(get_class($object));
$methods = array_flip($methods);
array_change_key_case($methods);
$result['methods'] = $methods;
}
$e = microtime(true);
echo "Duration get_*: " . ($e - $s) . "\n";
$s = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$class = get_class($object);
$r = new ReflectionClass($class);
$methods = array();
foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
$methods[strtolower($method->getName())] = true;
}
$props = array();
foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
$props[$property->getName()] = true;
}
}
$e = microtime(true);
echo "Duration reflection: " . ($e - $s) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment