Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105629 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 32920 invoked from network); 7 May 2019 18:10:08 -0000 Received: from unknown (HELO localhost.localdomain) (76.75.200.58) by pb1.pair.com with SMTP; 7 May 2019 18:10:08 -0000 To: internals@lists.php.net References: Date: Tue, 7 May 2019 16:14:03 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Posted-By: 94.1.167.52 Subject: Re: Error instead of returning false From: markyr@gmail.com (Mark Randall) Message-ID: On 07/05/2019 15:22, Gert wrote: > My idea, extremely summarized, would be to take the functions that > return false/null when they 'error', and instead make them actually > throw an error. It comes up on various discussion forums every other month. There's a few downsides to it, but mainly that any existing error handling would need to be re-written as tests for the false value would never be reached after the exception was thrown. It's a bit less of a hassle for more modern PHP design practices, as any warnings are globally promoted to exceptions using set_error_handler in userspace, but this too has its problems (for example in third party libraries). IIRC, most functions are at least checking their input argument types now and throwing InvalidArgumentExceptions, and the odd few like json_decode have their exception behaviour, provided its flagged. The new security focused stuff always throws (such as if there's not enough entropy available). I don't think it's in any way practical to add throw-or-not flags to every other internal function that could error out, and so there are 4 options that I can see: 1. Make no change. 2. Everything throws. People have to re-write. The compiler would probably need to further inspect the AST at compile time, and throw a depreciation warning if the result of an error-able internal function was tested for false. 3. Class (or namespace) level declares to throw on internal function errors. 4. Leave the existing functions as-is, finally claim the \php namespace, alias all of the internal functions into it, and make their default, unchangeable behaviour be to throw a RuntimeException on error. -- Mark Randall