Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80837 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11297 invoked from network); 19 Jan 2015 17:04:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jan 2015 17:04:37 -0000 Authentication-Results: pb1.pair.com smtp.mail=mike.php.net@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mike.php.net@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.52 as permitted sender) X-PHP-List-Original-Sender: mike.php.net@gmail.com X-Host-Fingerprint: 209.85.215.52 mail-la0-f52.google.com Received: from [209.85.215.52] ([209.85.215.52:65335] helo=mail-la0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B0/DC-64889-3293DB45 for ; Mon, 19 Jan 2015 12:04:36 -0500 Received: by mail-la0-f52.google.com with SMTP id hs14so29870165lab.11 for ; Mon, 19 Jan 2015 09:04:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=zo2r8m6Q1h69k0beY3Dpe67vyhp/0Barx/AmVyyDsL0=; b=bqljs+BDDDOexfRMUPJUFGfsJcXNAcaMFK/Sjm2GjXS1Mad0Vvsgx7fA07IIaWyKjv Wpuk7Q6rpHL+Kv4YTjzoCk70AF6bcAjfhxglqdMz2pEfa/CkYlZSszdV1XZXD8Ute8x1 G6gV11dWqzDHVVGkFKUYXhc0U/DM2cGD6siC/aLIOx932Azh779uAlUS6lsOxk99v9tF DfzJJ31ldgdTUqNi9/cHrUVIPd/Jf6U7MbFtdH0N0yIZAP29wAVlHMagvZngCyKE0/LI ubkWYb1YIJmrOvzBejGAjmU9aJhXYeVxM/zck6yE3U+NuLHEMZwmpOmfkBnIbLwFQ9NO b86g== MIME-Version: 1.0 X-Received: by 10.152.1.2 with SMTP id 2mr26370999lai.89.1421687072962; Mon, 19 Jan 2015 09:04:32 -0800 (PST) Sender: mike.php.net@gmail.com Received: by 10.115.1.132 with HTTP; Mon, 19 Jan 2015 09:04:32 -0800 (PST) Received: by 10.115.1.132 with HTTP; Mon, 19 Jan 2015 09:04:32 -0800 (PST) In-Reply-To: References: <54BC9909.6090100@gmail.com> Date: Mon, 19 Jan 2015 18:04:32 +0100 X-Google-Sender-Auth: 4-Q7OYfyxEGkGsiSgC1V0ZRukd4 Message-ID: To: Ferenc Kovacs Cc: Dan Ackroyd , PHP Internals , Stanislav Malyshev Content-Type: multipart/alternative; boundary=089e013c6672f7541d050d0450f6 Subject: Re: [PHP-DEV] Class constructor behaviour From: mike@php.net (Michael Wallner) --089e013c6672f7541d050d0450f6 Content-Type: text/plain; charset=UTF-8 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 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=119263748000001&r=1&w=2 ) 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 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 = 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). > Definitely my take on this, too. If userland constructors cannot return anything than an instance of the class, why should internal? --089e013c6672f7541d050d0450f6--