Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64590 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59017 invoked from network); 6 Jan 2013 01:39:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Jan 2013 01:39:29 -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.215.49 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.215.49 mail-la0-f49.google.com Received: from [209.85.215.49] ([209.85.215.49:60483] helo=mail-la0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BF/92-62408-EC5D8E05 for ; Sat, 05 Jan 2013 20:39:28 -0500 Received: by mail-la0-f49.google.com with SMTP id fk20so12353717lab.36 for ; Sat, 05 Jan 2013 17:39:23 -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=RQb8tTMIkwH0RO6yXwbpFhOD016voNFSlZ6+vnjzuv8=; b=nV6LyDJtPORJPnGuM3S78TMI1GDMjg/L5OwoRba9U3e6vSJN7OrQNPqbmlF4AIMasS F9tpRJL/yyL48BUu0knlQjYjKeEzLa3T3Tjz1qO4CSKZvUf+m+bTyA26ES86x8DuXwI7 oAxvfReAZQI67cHKDC5b3vq1sjjmt/OD4O8jfvlBgmo3DfI5sBxyR2K8JVv72q02grHB iQX3YWX4+iu+A75vh64K3t7kOGs+yPpng235fiIxFs8sZGUMDwzy4z+8F9cEPHY0vELc jvKtkmx/pJMBPU4DKq39WucyduXmrW7OdabhrwVhe2EZ+cJxhxkk/U7P37/KP5UxnT5g NG2Q== MIME-Version: 1.0 Received: by 10.112.98.232 with SMTP id el8mr11068333lbb.121.1357436362898; Sat, 05 Jan 2013 17:39:22 -0800 (PST) Received: by 10.112.4.168 with HTTP; Sat, 5 Jan 2013 17:39:22 -0800 (PST) In-Reply-To: <50E8CDC8.6080809@sugarcrm.com> References: <19D80B01-BE88-4119-8A15-B631A96060E5@mrclay.org> <50E8AA4D.5090207@sugarcrm.com> <50E8C2BA.9080309@sugarcrm.com> <50E8CDC8.6080809@sugarcrm.com> Date: Sun, 6 Jan 2013 02:39:22 +0100 Message-ID: To: Stas Malyshev Cc: Anthony Ferrara , PHP internals Content-Type: multipart/alternative; boundary=f46d0401f8e1373e4d04d294c8e1 Subject: Re: [PHP-DEV] [RFC] Alternative typehinting syntax for accessors From: nikita.ppv@gmail.com (Nikita Popov) --f46d0401f8e1373e4d04d294c8e1 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jan 6, 2013 at 2:05 AM, Stas Malyshev wrote: > You convinced me that this feature is actually harmful to PHP - dealing > with unset() throwing fatal errors is much worse than occasional empty() > check. This is a huge landmine - now any code that unsets object > property can return fatal error, and there's, unlike is_null, not even > any way to check it. Wait, what are you talking about here? About this particular proposal or the accessors RFC in general? The fact that unset() can throw a (catchable) fatal error is just the same without this particular syntax. If you define a property with an object-typehinted set accessor, then unset() will not be possible (throwing a catchable fatal). And I really don't see what's wrong with this. Unset() does not and should not work silently on anything you pass it. Some things just can't be unset() and we shouldn't pretend like they can just by silently ignoring. On this topic, a tangentially related note: The current accessors RFC will make an unset() call on a property without setter just do nothing *without an error*. I don't know why you would think this is a good idea. I honestly can't see how just silently ignoring instructions is any good. If something can't be unset PHP should tell you so, rather than leaving you with an (incorrect!) sense of the operation being performed successfully. >> The timeframe in which it is NULL is usually contained to the constructor. > So what? Objects are constructed all the time, and constructors can call > any other code. Also, nothing really guarantees the constructor actually > has initialized the variable - the constructor author could have > forgotten it just as you could have forgotten to initialize it without > "no NULL" requirement - so when you are using the API of the object, you > can not know if the constructor initialized it or not. And if you're > sure your code is right - you don't need null checks anyway, as you know > your code never assigns null there. There are some things one should address here: 1. First of all, there never is a guarantee for everything. All kinds of typing mechanism can only go so far. They are supposed to help you find mistakes and prevent issues, but they can't do *everything*. If you know some magic method to prevent all possible bugs in software, please let me know. Typehinting is just another defense mechanism preventing many common faults, but it can't prevent everything. 2. Secondly, the main purpose of typehints is to prevent people setting incorrect values. If you know that only a "DateTime" instance is valid and that NULL is not valid, then you should allow only that, only the DateTime. Allowing NULL absolutely doesn't make sense (as it's not a valid value for your application), so I'm not sure why you insist on allowing it. unset()ing it doesn't make sense, so we shouldn't allow it. Your argumentation goes along the lines of "It's possible that this property could maybe end up being NULL, so it does not make sense to prevent people from assigning NULL". The second part of that sentence does not follow from the first part. The fact that it can be NULL before initialization does not mean that we shouldn't prevent API users from doing a NULL assignment. 3. The constructor is typically (or at least that's how it *should be*) small and doesn't do work, so there isn't much to do wrong. But sure, it can still happen that you forget to initialize something, no arguing that. The typehint on the property will not prevent this, absolutely. Still it will prevent all other incorrect assignments to the property, which is exactly its goal. I'd summarize this as follows: The typehint prevents all incorrectly-typed assignments, but it does not prevent missing initialization. Your argumentation is that because it can't prevent missing initialization, we shouldn't bother with preventing incorrect assignments either. I hope you realize that this makes no sense. Nikita --f46d0401f8e1373e4d04d294c8e1--