-
-
Save arnaud-lb/1382400 to your computer and use it in GitHub Desktop.
get_ vs reflection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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