I'm wondering if there's any performance faulting caused by using 'assert' when ASSERT_ACTIVE
is set to 0 (assert_options(ASSERT_ACTIVE, 0);
.
For example, if I have a huge project with lots of asserts in it, will it be any slower than if those asserts wouldn't be there? On most languages, there would not be any difference between these 2 cases, but I'm thinking this is because they're compiled, and not executed on the fly as PHP is.
In case there's no performance difference, is it possible to create functions similar to assert
in PHP, such that when a flag/variable is unset, all calls to that function is completely ignored? (Such a function could be used to make development/debugging easier, but would not have any value on a launched website).
The performance hit wouldn't really be significant...use it all you want.
To create a similar function which ignores calls when a flag is unset...see below.
define('ASSERT_ENABLED', true);
function assertEquals($a, $b)
{
if( !defined('ASSERT_ENABLED') || !ASSERT_ENABLED ) {
return;
}
if($a !== $b) {
throw new \RuntimeException("Failed asserting that $a === $b");
}
}