Skip to content

Instantly share code, notes, and snippets.

@natebrunette
Created September 14, 2016 17:04
Show Gist options
  • Save natebrunette/7f4d5752325dc51c6142d6cb37724576 to your computer and use it in GitHub Desktop.
Save natebrunette/7f4d5752325dc51c6142d6cb37724576 to your computer and use it in GitHub Desktop.
<?php
class MyClass
{
private $foo;
public function setFoo($foo)
{
$this->foo = $foo;
}
public function getFoo()
{
return $this->foo;
}
}
$myClass = new MyClass();
$count = 1000000;
$startTime = microtime(true);
for ($i = 0; $i < $count; $i++) {
$myClass->setFoo(1);
$foo = $myClass->getFoo();
}
$endTime = microtime(true);
echo 'Setters/getters: ', $endTime - $startTime, PHP_EOL;
$startTime = microtime(true);
for ($i = 0; $i < $count; $i++) {
$r = new ReflectionClass('MyClass');
$p = $r->getProperty('foo');
$p->setAccessible(true);
$p->setValue($myClass, 1);
$foo = $p->getValue($myClass);
}
$endTime = microtime(true);
echo 'Reflection: ', $endTime - $startTime, PHP_EOL;
$startTime = microtime(true);
for ($i = 0; $i < $count; $i++) {
Closure::bind(function () {
$this->foo = 1;
$foo = $this->foo;
}, $myClass);
}
$endTime = microtime(true);
echo 'Closure: ', $endTime - $startTime, PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment