Re: Assertions

From: Date: Thu, 17 Oct 2013 10:51:03 +0000
Subject: Re: Assertions
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Thu, Oct 17, 2013 at 9:25 AM, Joe Watkins <[email protected]> wrote:

> Morning All,
>
>         I'd like to draw some attention to how poor assertions are in PHP.
>
>         The current assertion API is not usable in my opinion; it has a
> considerable overhead, relies on eval(), and is generally poorly
> implemented.
>
>         I have done some work toward implementing assert at the Zend
> level, giving assertions a more modern, usable feel to them.
>
>         https://github.com/krakjoe/**php-src/compare/assert<https://github.com/krakjoe/php-src/compare/assert>
>
>         This implementation of assert removes the old implementation and
> associated functions and INI settings, and replaces it with a single INI
> setting to control assertion compilation.
>
>         Failed assertions throw an AssertionException (which extends
> ErrorException with a severity of E_ERROR), setting the message of the
> exception to the expression asserted.
>
>         The syntax of assertion is the same as [all] other languages:
>
>         T_ASSERT expr ';'
>
>         This means that assert("some code here") will pass assertion
> causing no error, because strings are no longer treated as code, because
> eval is evil();
>
>         Setting zend.assertions=0 system configuration setting will stop
> ZEND_ASSRT compilation.
>
>         So, we have:
>
>         try {
>                 assert (PHP != JUNK);
>
>
>         } catch(AssertionException $ex) {
>                 printf("Assertion failed: %s\n", $ex->getMessage());
>                 printf("Something is horribly wrong ...\n");
>         }
>
>         Better, no ??


I like the idea.
However, there is always the debatte about if a Core feature should throw
an Exception or generate an error.
In our current system, they don't throw Exceptions but generate errors.
Also : someone could use a callback, and then make them throw exceptions if
he wants to ; the callback on assertion fail, which IMO is a good feature,
has dissapeared in your patch.

Julien.Pauli


Thread (36 messages)

« previous php.internals (#69599) next »