Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87754 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78731 invoked from network); 14 Aug 2015 15:00:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Aug 2015 15:00:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.169 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.212.169 mail-wi0-f169.google.com Received: from [209.85.212.169] ([209.85.212.169:38757] helo=mail-wi0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7B/50-10952-9820EC55 for ; Fri, 14 Aug 2015 11:00:26 -0400 Received: by wicja10 with SMTP id ja10so23625211wic.1 for ; Fri, 14 Aug 2015 08:00:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=HKOVZmZi77sJNTekexdtA2kfoEnCqo9BLGbMe763ZHk=; b=zFYlXnBDZ5CUFukpksp9Bgbf+/uvSLLTAuawBbw7zTg0v2+0UW7ESlPBR3eeX1yKEF fJoFXXJ5bh36f3pcyqvb/etP6I2Ng9NaYx25BQntOKA7q3IJCdVeZvbtXx3ULN7fBlmz nahEJpXFoG4aHW4d1FZV1DBO1gxf4WKPSdF3ttkVkqq2dQu+PnTRvsdtbyQ5fKbsQp7H 2HfQOhDgOwdpdnbhztJIxpjokuGR8YmCQf4QhwC2+aB3F6Q2P9ZSms1y1f8mpb1QOQ+W nCIvKbH4Q0Gt7oUeq9UBcM1SIndXuiR4aGHOVUTh2FOtNDXrXM+m/jxqPfWrWDqApm39 VbRw== MIME-Version: 1.0 X-Received: by 10.194.94.73 with SMTP id da9mr626753wjb.97.1439564111152; Fri, 14 Aug 2015 07:55:11 -0700 (PDT) Received: by 10.27.140.215 with HTTP; Fri, 14 Aug 2015 07:55:11 -0700 (PDT) In-Reply-To: References: <003401d0d66d$385af9d0$a910ed70$@belski.net> Date: Fri, 14 Aug 2015 16:55:11 +0200 Message-ID: To: Davey Shafik Cc: Anatol Belski , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=047d7bb04efe7a0add051d46a366 Subject: Re: [PHP-DEV] Warning (5.x) -> TypeError (7) for internal Constructors From: nikita.ppv@gmail.com (Nikita Popov) --047d7bb04efe7a0add051d46a366 Content-Type: text/plain; charset=UTF-8 On Fri, Aug 14, 2015 at 12:24 PM, Davey Shafik wrote: > > So from this POV it is good, IMO. Namely, to know that it is the concrete > case delivering the concrete exception type instead of abstract Exception, > IntlException, PharException, etc. The chosen exception class name might > make you think that it's like enforcing strict mode, but that's not the > case. Hm, so question is - are exception class names really bound to > whether strict mode is used or not? > > I don't disagree with this on principle, consistency is good, but now we > actually have LESS consistency because the constructor can throw one of > many exceptions. For example, PDO can throw a \TypeError or a \PDOException > on connection failure, or if the constructor fails in some other way. This > leads to code like this: > > try { > new PDO([]); > } catch (\TypeError $e) { > > } catch (\PDOException $e) { > > } > > With the potential for duplication in the error handling (in both catch > blocks), OR code like this which is overly generic and will potentially > catch other random stuff: > > try { > new PDO([]); > } catch (\Throwable $e) { > // must be throwable as it's the only common thing between \TypeError > and \PDOException > } > > This is why I say the previous behavior should be preserved: Warning for > type mis-match, with a (in this case) \PDOException if the constructor > fails (possibly as a result of the mis-match, or for any other reason) > UNLESS strict types are on, then a \TypeError should be thrown, and it > wouldn't attempt to execute the constructor and no \PDOException would be > thrown. > > This then gives us the following flow: > > try { > new PDO([]); // Warning, array given, string expected > } catch (\PDOException $e) { > // maybe connection failed, or constructor failed because we passed an > array! > } > > OR we enable strict types and you basically need the examples shown > originally, but at least that's a conscious choice and expected behavior. > > I believe that the goal should be: don't surprise your users, and this > change fails that. The new behavior is not what I think most people would > expect coming from 5.x, and that is a bad thing. > Why do you have an expectation that a function should throw one exception type and one type only? It is customary that different causes for an exception will also result in different exception types. Here TypeError is thrown if a wildly inappropriate type is passed, say an array instead of a string. A PDOException on the other hand is thrown if some failure specific to PDO's domain occurs, say a non-existing socket or invalid user name. A programmer wishing to catch a PDOException is unlikely to be interested in catching the kind of error that TypeErrors signify. The motivation behind using TypeError here is twofold. Firstly, it is an error type that exists specifically to be thrown for zpp failures. Secondly, and more importantly, this avoid throwing different exception types between weak and strict mode. If we didn't throw TypeError here, then switching from weak to strict would change what was previously a PDOException to a TypeError -- which I would consider to be rather unexpected and suboptimal. Nikita --047d7bb04efe7a0add051d46a366--