Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80805 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41417 invoked from network); 19 Jan 2015 11:30:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jan 2015 11:30:20 -0000 Authentication-Results: pb1.pair.com header.from=tyra3l@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tyra3l@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.53 as permitted sender) X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 74.125.82.53 mail-wg0-f53.google.com Received: from [74.125.82.53] ([74.125.82.53:49698] helo=mail-wg0-f53.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BA/1F-64889-8CAECB45 for ; Mon, 19 Jan 2015 06:30:18 -0500 Received: by mail-wg0-f53.google.com with SMTP id x13so31100534wgg.12 for ; Mon, 19 Jan 2015 03:30:11 -0800 (PST) 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=e9KJ/Eq1SmKhF7nGKFZQbeKWHK7xt4Iqk7HNhybWS2g=; b=JHMpW6a0UmS/nZecwK/uEmKqJ7N4ktxiXqCK6u3TCreExMPeKN0Z1E5RK3CCd6mFv/ funv0+ya6zVkRBP2w377I8qkyvrLPZYmhzZcstTYGIr6/KTz/gr/6Vjes/ZXfgPwa+dd PRnaCyECnXOm/xYd6ECNnO1RtbaDr6/C/s0ANnTsp+YXfNo/FJ2R/JlveOleNeK+6pLl Gf05fJRkcLAB7fnGLRUvdKN3wDjqeK7z78025YpPWMqB9v2DHsnLSKk8MR8Ukn1Du8qc n6BgPTb+NEjqGYGuPSavJYfmQEjN+zNRC6vw/X1pd1EoqPphwjV5HdLmRHtxwfUGLYAK ny/A== MIME-Version: 1.0 X-Received: by 10.194.175.102 with SMTP id bz6mr57066021wjc.120.1421667009705; Mon, 19 Jan 2015 03:30:09 -0800 (PST) Received: by 10.180.88.33 with HTTP; Mon, 19 Jan 2015 03:30:09 -0800 (PST) In-Reply-To: <54BC9909.6090100@gmail.com> References: <54BC9909.6090100@gmail.com> Date: Mon, 19 Jan 2015 12:30:09 +0100 Message-ID: To: Stanislav Malyshev Cc: Dan Ackroyd , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=089e0149365a1a4079050cffa577 Subject: Re: [PHP-DEV] Class constructor behaviour From: tyra3l@gmail.com (Ferenc Kovacs) --089e0149365a1a4079050cffa577 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, Jan 19, 2015 at 6:41 AM, Stanislav Malyshev wrote: > Hi! > > > This is pretty horrible and should be fixed by making sure that > > I don't see why it's so horrible. In fact, handling null is easier than > handling exception (and faster too, probably). Many functions that try > to create objects return null or false when unable to. Of course, we > could change it by introducing exceptions to core, but I don't see why > do it peacemeal then. > > > constructors either return an object or throw an exception. > > Additionally the exception policy for core (that was previously > > discussed here: http://marc.info/?t=3D119263748000001&r=3D1&w=3D2 ) sho= uld > > be updated so that any constructor returning NULL is considered a bug, > > no matter what the ini settings are. To be clear, procedural code > > should behave as before, with users expected to check for errors. > > This would be a BC break for people who are handling the constructor > > returning null currently, as they would need to wrap that code with an > > try/catch block. > > That means the same failure of collator construction now needs to be > handled in two different ways. > > > Constructors give warning, but are then in an unusable state > > ------------------------------------------------------------ > > Several constructors check the parameters that they are given.....and > > then just give a warning when they are not acceptable. e.g. > > These should be fixed to either return null or throw exception, but the > behavior should be unified for everybody. > > > Again, this should be fixed by changing the constructors to throw an > > exception if the input parameters are not acceptable. > > The next question would be - if the ctors throw an exceptions, why > factory methods shouldn't? They are doing the same work essentially. > > > Constructor gives error > > ----------------------- > > > > Some constructors check the parameters they are given, and then emit > > either a 'Catchable fatal error' error or other error e.g. > > This is even bigger can of worms. If we wrote PHP today from scratch, > catchable fatal errors would be exceptions. But they are not, so we need > to deal with it - by either leaving them as is, or converting them all > together. > > > > > > $foo =3D new IntlGregorianCalendar(new StdClass); > > //Output: Catchable fatal error: Object of class stdClass could not be > > converted to string in.. > > This is not property of the ctor, it's property of parameter handling. > I.e. if you do it for IntlGregorianCalendar, you should do it for every > class that gets incompatible parameters. And probably user-space funcs > too since there's no reason for them to behave differently. I actually > wouldn't mind that, but this is yet bigger can of worms as it requires > refactoring a lot of assumptions on how parameters are parsed. > > > i) Can anyone see a big hurdle in fixing these behaviours, other than > > it being a BC break for people who are currently relying on these > > behaviours ? > > Yes. The first hurdle is to understand which of these behaviors need > fixing, and how, which you seem to dismiss as obvious but it's really > not. The second is to do it in consistent manner so it doesn't again > come out as part of the functions do one thing, another part another, > and third part something else different. > > > ii) Are there any other bad behaviours that people are aware of that > > ought to be fixed at a major version? > > I think we have enough for now to discuss here, so if we want do discuss > other behaviors better to open separate thread. > > -- > Stas Malyshev > smalyshev@gmail.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > Hi, AFAIK (based on http://www.mail-archive.com/internals@lists.php.net/msg43237.html and other discussions) errors in constructors should throw exceptions (even for classes in core). --=20 Ferenc Kov=C3=A1cs @Tyr43l - http://tyrael.hu --089e0149365a1a4079050cffa577--