Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84150 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46082 invoked from network); 2 Mar 2015 07:57:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Mar 2015 07:57:30 -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 74.125.82.169 as permitted sender) X-PHP-List-Original-Sender: mike.php.net@gmail.com X-Host-Fingerprint: 74.125.82.169 mail-we0-f169.google.com Received: from [74.125.82.169] ([74.125.82.169:35556] helo=mail-we0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EB/E3-25952-6E714F45 for ; Mon, 02 Mar 2015 02:57:28 -0500 Received: by wevl61 with SMTP id l61so31605140wev.2 for ; Sun, 01 Mar 2015 23:57:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=e8ShQoN0yTPoaFwZE80oJ3NsqB8wJBtbndKQhYHfJpM=; b=V1SpJGIZyD/8+TD7FhwLWS0E4aEs2iuHKzESbQIIbgjHq24AOfY/t7rKmQ/RWNX+lo kslFDxNhw8149k4ysBcbu/Kv0k4zZd5TQpaWq6IQWcs4GxScnsRV806G1A9OMs3GDpqp jB7Mai3zytxy2ooc4uDK4Go1wi7rNZ25LvFTZfTf2WQiNJ27ZXbtszsWb9Me0c+np+QZ k/gW7rVzZeKQEq7ibuHvZftSaCpn+h2N1IHoKHDI2x6R8hbdyyrrgOtkrYQiaMFGzVLp HgeTxesdIhlVFBnyuFgRR77peZyyq25ORxa1B26v+B15cDSI9WynVG21k0GaJywUYCsC agig== X-Received: by 10.180.198.211 with SMTP id je19mr24878318wic.37.1425283043482; Sun, 01 Mar 2015 23:57:23 -0800 (PST) Received: from [192.168.2.121] (89-104-28-113.customer.bnet.at. [89.104.28.113]) by mx.google.com with ESMTPSA id ga8sm14895116wib.6.2015.03.01.23.57.22 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Mar 2015 23:57:22 -0800 (PST) Sender: Michael Wallner Message-ID: <54F417E1.8010703@php.net> Date: Mon, 02 Mar 2015 08:57:21 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Stanislav Malyshev , Dan Ackroyd , "internals@lists.php.net" References: <54F38D29.9080007@gmail.com> In-Reply-To: <54F38D29.9080007@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC][DISCUSSION] Constructor behaviour of internal classes From: mike@php.net (Michael Wallner) On 01/03/15 23:05, Stanislav Malyshev wrote: > Hi! > >> This email is to announce the formal opening of discussion for an RFC >> to clean up the behaviour of the constructors shown by several >> internal classes. >> >> https://wiki.php.net/rfc/internal_constructor_behaviour > > I'm not sure why this RFC assumes the exception is much better than > returning null. Exceptions are harder to handle (requires separate > syntax construct) and they are more expensive at runtime (must create an > object, collect the backtrace, etc.). Also, not catching the exception > immediately usually will lead to a complete application failure (usually > with an obscure error message like "internal operation failed", since > displaying raw exceptions to the user can be unsafe) instead of > finer-grained handling. > It is a long standing tradition in PHP that operations that can be > expected to fail - like fopen(), date_create(), etc. - do not produce > fatal errors but instead return error values which can be checked for > and handled. It is true that some SPL classes departed from that, but > this IMO was not an improvement but rather an annoyance that each time > you use an SPL thing you have to write it in a try/catch block. That > leads to much more boilerplate code than necessary. > > Now I understand that PHP model of doing things may be unusual for some > people that come from other language background where everything throws > exceptions for any slightest reason and there's no other means of error > checking. But I think we can do better rather than that and have more > flexible error handling - even if it would require people to be > introduced to a concept they were not familiar with before coming to > PHP, such as not using exceptions for everything. > I'm not sure I can understand your crusade against this topic. Consistency with userland is beneficial, because the majority of PHP developers probably do not expect `new` to yield anything than a concrete instance or an exception. Quoting php.net/manual: To create an instance of a class, the new keyword must be used. An object will *always* be created *unless* the object has a constructor defined that *throws an exception on error*. Emphasis mine. Do you know of any other case where a `new` operator in an object oriented context can return NULL, except C++ where you have to explicitely ask for that behavior? -- Regards, Mike