Re: [PEPr] Comment on PHP::Assert

From: Date: Thu, 11 Aug 2005 19:15:07 +0000
Subject: Re: [PEPr] Comment on PHP::Assert
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-39330@lists.php.net to get a copy of this message
Travis Swicegood wrote:
Jesper Veggerby wrote while I was writing this reply:
... but since assert() drops a warning and returns true or false, I'd personally prefer the simple "if"-check and the exception, which you can at least catch in a try-catch (somewhere in the call-stack!) Not as assert() where you only can "catch" it at the assertion point (also with and if (and you have to use @assert()).
You can retool assert() to do whatever you like. See assert_options() for changing the callback. I've created a wrapper for throwing an AssertException. That said, assert() in general should always pass. By failing, it means your code would not be able to run as expected (i.e., an extension is missing, an improper argument has been passed in, etc.). -Travis
While you were writing this I did some testing as well, testing mainly on assertions using exceptions, since I think they provide the greatest value (and possibly recovery!). Coming from an OO world and vouching for OO I think Exceptions are better than PHP errors (but that's just my point of view) I did the following test "suite":
    try {
        if (!@assert(true)) {
            throw new Exception('Test');
        }
    }
    catch (Exception $ex) {
    }
100.000 iterations = 2.9696 secs
    try {
        if (!(true)) {
            throw new Exception('Test');
        }
    }
    catch (Exception $ex) {
    }
100.000 iterations = 1.1123 secs!!!!!!!!!!!!!!!!!!!!!!!!! function throwException($script, $line, $message) {
    throw new Exception($script);
} assert_options(ASSERT_CALLBACK, 'throwException'); and 100.000 iterations of:
    try {
        @assert(true);
    }
    catch (Exception $ex) {
    }
took 2.608 seconds Where again 100.000 iterations of
    try {
        Assert::thatTrue(true);
    }
    catch (Exception $ex) {
    }
took 5.3306 seconds (Sorry my old machine so I only tested 100.000 in the same time you did 1.000.000, but the picture is still fairly clear). regards Jesper

« previous php.pear.dev (#39330) next »