Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Created May 26, 2021 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krakjoe/cef6452281624bdf1b46788f52a01521 to your computer and use it in GitHub Desktop.
Save krakjoe/cef6452281624bdf1b46788f52a01521 to your computer and use it in GitHub Desktop.
<?php
class Foo {
private int $property;
public function __construct() {
$this->reflector = new ReflectionProperty($this, "property");
$this->reflector->setAccessible(true);
}
public function test() {
return is_initialized($this, "property");
}
public function testReflection() {
return $this->reflector->isInitialized($this);
}
}
$foo = new Foo;
$iterations = 1000000;
$start = microtime(true);
for ($count = 0; $count < $iterations; $count++)
$foo->test();
printf("is_initialized: %.5f seconds\n", microtime(true)-$start);
$start = microtime(true);
for ($count = 0; $count < $iterations; $count++)
$foo->testReflection();
printf("reflection: %.5f seconds\n", microtime(true)-$start);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment