Skip to content

Instantly share code, notes, and snippets.

@krakjoe

krakjoe/is_initialized.php Secret

Created May 26, 2021
Embed
What would you like to do?
<?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