Last active
August 29, 2015 14:21
-
-
Save cboden/e18ee4b3af40dbc3daa7 to your computer and use it in GitHub Desktop.
Exceptions...
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 ExceptionFactory implements ExceptionFactoryInterface { | |
public function __invoke($msg = '') { | |
return new \Exception($msg); | |
} | |
} |
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 | |
interface ExceptionFactoryInterface { | |
/** | |
* @return \Exception | |
*/ | |
public function __invoke(); | |
} |
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
Throw new Exception: 2.838799 | |
Throw $exception: 0.540748 |
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 | |
$theThing = function(ExceptionFactoryInterface $factory) { | |
throw $factory(); | |
}; | |
$sef = new ExceptionFactory; | |
$nef = new StacklessExceptionFactory; | |
echo "Throw new Exception: "; | |
$s = microtime(true); | |
for ($i = 0; $i < $t; $i++) { | |
try { | |
$theThing($sef); | |
} catch (Exception $e) { | |
} | |
} | |
printf("%0.6f\n", (microtime(true) - $s)); | |
echo 'Throw $exception: '; | |
$s = microtime(true); | |
for ($i = 0; $i < $t; $i++) { | |
try { | |
$theThing($nef); | |
} catch (Exception $e) { | |
} | |
} | |
printf("%0.6f\n", (microtime(true) - $s)); |
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 StacklessExceptionFactory implements ExceptionFactoryInterface { | |
private $held; | |
public function __construct() { | |
$this->held = new \Exception; | |
} | |
public function __invoke() { | |
return $this->held; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment