Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80839 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15329 invoked from network); 19 Jan 2015 17:37:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jan 2015 17:37:02 -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.52 as permitted sender) X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 74.125.82.52 mail-wg0-f52.google.com Received: from [74.125.82.52] ([74.125.82.52:51966] helo=mail-wg0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C9/8D-64889-CB04DB45 for ; Mon, 19 Jan 2015 12:37:01 -0500 Received: by mail-wg0-f52.google.com with SMTP id l2so10933957wgh.11 for ; Mon, 19 Jan 2015 09:36:56 -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=/CccHvwg4BjnHOCmAO8ChDImYDR3NhXsSyvahkrmHeI=; b=WwzsJjCdDsR8qZ0yKjtYM+8WtWPMGXRNmf6npRUe0Hpr5oMDS0CQXBd0QXxQi0SG0O uqeUfh887nhjwjvGeRVJhAFgKVI/eHh0kfPgzQ1oj3kqEQKp76+PrU1l2TLYLNSsenA8 7HGQe2IqMFQQYDrFuHL9c1ilP9IrkWv0Mq4hPFgGiQxV08Q2F32Q82yDq2Q2LuU3q8/o E7JSW9AJ6vpz+fW6W4PDlz5P7XLxCRZjoLlQTgRYv7k+QF+/iL9dGVpYJ9EhdbLx8KgO Hp6NxFOilv9dejmOh8XN5X1jOrlwR8TIqFtRGWeQ1KlswCTbAjT5HBZmz3UWF1Lgu0WN q72Q== MIME-Version: 1.0 X-Received: by 10.194.90.10 with SMTP id bs10mr27102675wjb.43.1421689016742; Mon, 19 Jan 2015 09:36:56 -0800 (PST) Received: by 10.180.88.33 with HTTP; Mon, 19 Jan 2015 09:36:56 -0800 (PST) In-Reply-To: References: <54BC9909.6090100@gmail.com> Date: Mon, 19 Jan 2015 18:36:56 +0100 Message-ID: To: Michael Wallner Cc: Dan Ackroyd , PHP Internals , Stanislav Malyshev Content-Type: multipart/alternative; boundary=047d7bd9058ed2fbc3050d04c4b1 Subject: Re: [PHP-DEV] Class constructor behaviour From: tyra3l@gmail.com (Ferenc Kovacs) --047d7bd9058ed2fbc3050d04c4b1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, Jan 19, 2015 at 6:04 PM, Michael Wallner wrote: > > On 19 Jan 2015 12:30, "Ferenc Kovacs" wrote: > > > > 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 th= an > > > handling exception (and faster too, probably). Many functions that tr= y > > > 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 wh= y > > > 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 )= should > > > > 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 constructo= r > > > > 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.....a= nd > > > > then just give a warning when they are not acceptable. e.g. > > > > > > These should be fixed to either return null or throw exception, but t= he > > > behavior should be unified for everybody. > > > > > > > Again, this should be fixed by changing the constructors to throw a= n > > > > 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 emi= t > > > > 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 al= l > > > 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 eve= ry > > > class that gets incompatible parameters. And probably user-space func= s > > > too since there's no reason for them to behave differently. I actuall= y > > > wouldn't mind that, but this is yet bigger can of worms as it require= s > > > refactoring a lot of assumptions on how parameters are parsed. > > > > > > > i) Can anyone see a big hurdle in fixing these behaviours, other th= an > > > > 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 tha= t > > > > 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). > > > > Definitely my take on this, too. If userland constructors cannot return > anything than an instance of the class, why should internal? > yep, ofc. this means that there are a couple of class method->function aliases which have to be changed/refactored, so calling the procedural counterpart won't throw exceptions, but thats shouldn't be a problem. --=20 Ferenc Kov=C3=A1cs @Tyr43l - http://tyrael.hu --047d7bd9058ed2fbc3050d04c4b1--