Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:69607 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33021 invoked from network); 17 Oct 2013 12:02:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Oct 2013 12:02:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=julienpauli@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=julienpauli@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.179 as permitted sender) X-PHP-List-Original-Sender: julienpauli@gmail.com X-Host-Fingerprint: 209.85.128.179 mail-ve0-f179.google.com Received: from [209.85.128.179] ([209.85.128.179:52001] helo=mail-ve0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0D/74-12663-2C1DF525 for ; Thu, 17 Oct 2013 08:02:11 -0400 Received: by mail-ve0-f179.google.com with SMTP id cz12so942990veb.38 for ; Thu, 17 Oct 2013 05:02:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=em2W6+D5cGoHHiqKJvBpbl1Dfh6JadWWcan9KQnaqLo=; b=ZiS7BfeFeunYggS4OJLcXl9/FGilEdXT+aUK25TDD7fi9cIZPq23g+sp0dUUkAMz0f g4hqEscz97nzpj7yU+k/IUD0wRWX5cyrdy3HYkpP6NXuqwT5gePk5bHmI9a9DUYuidQO nEZE6D9ll+mzt94pa4vovS7CU3ycQ1/zPbg1auN+4wKKaXbUllI+CfN8ph2lfU1/gJjV SMzYje6cC3oL4rjsTuDSqeRvp69Qj3TuEs2LmTjtFNLagzvP+3NKEhaRO5QOw4vMMOVC TDennNsj6XCMNS/Ol1dVBUu0JvHDPfEDcgmVAgW0FR4cFrfIlAgtt/vszJydM9/QNzRk tlrw== X-Received: by 10.220.1.203 with SMTP id 11mr6526725vcg.15.1382011327676; Thu, 17 Oct 2013 05:02:07 -0700 (PDT) MIME-Version: 1.0 Sender: julienpauli@gmail.com Received: by 10.220.73.197 with HTTP; Thu, 17 Oct 2013 05:01:27 -0700 (PDT) In-Reply-To: <525FC834.4060501@php.net> References: <525FC834.4060501@php.net> Date: Thu, 17 Oct 2013 14:01:27 +0200 X-Google-Sender-Auth: L-FNmWyFHMVAyUntYKgmjoKpBdM Message-ID: To: Joe Watkins Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a11c3d3f443084c04e8ee9660 Subject: Re: [PHP-DEV] Assertions From: jpauli@php.net (Julien Pauli) --001a11c3d3f443084c04e8ee9660 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Oct 17, 2013 at 1:21 PM, Joe Watkins wrote: > On 10/17/2013 11:51 AM, Julien Pauli wrote: > >> On Thu, Oct 17, 2013 at 9:25 AM, Joe Watkins 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 >>> >>> > >>> >>> >>> 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 >> >> Morning Julien, > > That was brought up in IRC yesterday, someone pointed out that > generators throw exceptions in some places (from an object method, but > still). So it's not unprecedented, and nobody has provided reason for the > preference to use errors over exceptions ... however, general case aside: > > Throwing exceptions here makes much more sense than errors, and is > the reason there is no need for callbacks; should you need to control the > flow of execution or take some action because of a failed assertion you can > catch the AssertionException and do that without cluttering ini and module > globals in order to do it. > > In general, of course, an assertion should abort execution, that's > a bit final for PHP and runs contrary to carry-on-executing-no-matter-**what, > which we like, on the interweb. But, should you need to take some action > and stop that exception from bubbling to the surface you should do that > with language constructs try/catch rather than dated callbacks, which don't > give you the same kind of recovery options so easily, they additionally > require a new scope, a new function entry, module globals for support, and > ini settings to configure ... > > That being said, I would rather reimplement the ability to invoke > a callback than scrap the idea all together ... but might we have something > a bit more PHP5 ?? > > assert( > $expression != false || > (CONSTANT_MASK & HAS_BITS), > function(){ > echo "oh rly?"; } > ); > > I still think it's a bit unnecessary, and this: > > try { > assert($expression != false || > (CONSTANT_MASK & HAS_BITS)); > > } catch (AssertionException $ex) { > echo "oh rly?"; > } > > Makes much more sense ... I would rather encourage or even require > that than reimplement callbacks ... they only seemed to exist to service > the current implementation. > This makes sense. However, I still continue to think that assertions should stay assertions, aka : run time fatal errors when enabled. For me, there is nothing to "catch", except the fact that the program has stopped somewhere, it tells you where, and why, and you have to fix it : that's an assertion (for me). Enabling or disabling it with an INI setting is just all right. Having assert() with exception makes the code much more heavy for me.