Re: Assertions

From: Date: Thu, 17 Oct 2013 13:49:57 +0000
Subject: Re: Assertions
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
(Stupid GMail, didn't reply automatically to the list)

On Thu, Oct 17, 2013 at 3:41 PM, Michael Wallner <[email protected]> wrote:
> On 17 October 2013 15:34, Marco Schuster <[email protected]> wrote:
>
>> I'd really like to do a simple try-assert-catch instead of writing e.g.
>> if(isset($_GET["foo"]) {
>>  throw new InvalidArgumentException('$_GET[foo] not set');
>> }
>>
>> which would become
>> assert(isset($_GET["foo"]));
>>
>> If you're doing a lot of isset-checks, then the assert-way is a bit of
>> an abuse of assert, but it results in far less boilerplate crap. Maybe
>> there's a way that e.g. one can write assertTerminate(cond) or
>> assertException(cond) to differ between the behaviors.
>
> I'm not sure I can follow...
>
> <?php
> function except($assertion, $description=null) {
>     if (!$assertion)
>         throw new Exception($description);
> }
>
> except(isset($_GET["foo"]))
This forces me to write a description and essentially duplicate the
assertion code, while an AssertionException already contains a
meaningful message ("Assertion in foo.php:1337 failed:
isset($_GET["bar"])!=TRUE").

Marco


Thread (36 messages)

« previous php.internals (#69620) next »