Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91760 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90179 invoked from network); 19 Mar 2016 07:00:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2016 07:00:02 -0000 Authentication-Results: pb1.pair.com header.from=pthreads@pthreads.org; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=pthreads@pthreads.org; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain pthreads.org from 209.85.161.170 cause and error) X-PHP-List-Original-Sender: pthreads@pthreads.org X-Host-Fingerprint: 209.85.161.170 mail-yw0-f170.google.com Received: from [209.85.161.170] ([209.85.161.170:34022] helo=mail-yw0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AB/52-03097-EE8FCE65 for ; Sat, 19 Mar 2016 02:00:00 -0500 Received: by mail-yw0-f170.google.com with SMTP id h129so164304400ywb.1 for ; Fri, 18 Mar 2016 23:59:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pthreads-org.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=mBn8gJb0a6fPbULF4HJk13SrZGr+2jlGWiNRG3By6qs=; b=jPtcBT0QnVDQuQf9iie1K7W14+XddtWFF47oefi04S5llXoCfL5oE8IxHh+/7J2yM6 vDcppDLDAcs1PmzVui1RsNSHLvWiVpmBxmzrKCTO0ahqLgNykaGIN9hOGb+VZhTvpNJ3 IuTsyhhMr5VyVTRR/XDYY6LeH/sACArmsisxPJRNYIx8h4FHaEzzka5lUBQvWVnkcjBB 8Uqs5oglQ32Pmu6uXB/ubuqKBDqUOzr5x3lezlAaLvk9nioElfM87D1BfEumGa9xlMue Q88qzse+Kp1L4ripZvB1FvHOhcFDcZDcISYm467We5l2Vu3hw1dXdb0u7HpUoV9pbHFu pOwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=mBn8gJb0a6fPbULF4HJk13SrZGr+2jlGWiNRG3By6qs=; b=NXRcKk2SAKUpELLcO32HZ+MhxlNHBR4jdxTA3n1VtdbWalS0PLtDURM0M05pQiBHOJ SZxJn/v+g/QEpM+P9g13S5Zvmp+L0/JD2aCRDnvDKvL2vqM/AhZ+z6jMFB0LRzPNre4I u80W0LC438+bJYvokuWZm+w8+wdSjKScd6bkts0XNtaMvTqEH0axfGaA1nZQAEv3x9j1 bDaQhr4uXMHjtQj9cMMIelVy9LekfSOvCMUsgKn1TugOG5vA2JvElZqx6vHu4gPdFhKs aOfpzAv0gV9Dxn3w4kya9MtJ4ZgrjSLRcKSgCqiAq5iQMLcMeLXeSZH+bZqyWKuKv5lC Ckdw== X-Gm-Message-State: AD7BkJIHks4TeeraIFk6bucjYctAguPx1Es+k2Z8MLKok4BW+PodiqciWa5YWqpu34PmxbWlcx9n0WDQRYPbLQ== MIME-Version: 1.0 X-Received: by 10.13.236.137 with SMTP id v131mr3860358ywe.102.1458370796026; Fri, 18 Mar 2016 23:59:56 -0700 (PDT) Received: by 10.129.81.133 with HTTP; Fri, 18 Mar 2016 23:59:55 -0700 (PDT) X-Originating-IP: [86.191.46.104] In-Reply-To: References: Date: Sat, 19 Mar 2016 06:59:55 +0000 Message-ID: To: Lee Davis Cc: Phil Sturgeon , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=94eb2c086cd2400582052e616957 Subject: Re: [PHP-DEV] [RFC Discussion] Typed Properties From: pthreads@pthreads.org (Joe Watkins) --94eb2c086cd2400582052e616957 Content-Type: text/plain; charset=UTF-8 Morning Lee, Phil is in a field somewhere, but I don't want to leave you hanging on for an answer ... so ... class Foo { public $bar; public function __get($name) { return 123; } } $foo = new Foo(); var_dump($foo->bar); You should not expect __get to be called in this example code; __get is only invoked when the property is undefined. That's what unset does; It doesn't just destroy the value, but tells the engine that the property is not declared (leaves it undef), which is what results in the subsequent invocation of __get upon access to the unset property. Accessing the property before it was unset though, as it always did, attempts to read the default value (null). We haven't changed any of that ... If $bar were typed, it would obviously be unacceptable to return anything other than a value of the declared type upon access, so we must not allow return to happen if there is no value on a typed property, because it's an obvious violation. Accessing a typed member before there is any possible chance the variable was set is a programmer error - colloquially known as "use before initialization" - a very common programming error, which we helpfully raise an exception for. Accessing a typed member after they were unset is another programming error - "use after free" - again, we helpfully raise an exception. In both of these circumstances, raising an exception is the only possible action; Anything else really would constitute a change to the way objects work, and or a violation of the type declared. The most you can say about this, is that it's strange that the same exception is raised for both cases, which we're aware of; Right now, it's impossible to distinguish between the two states. The things you can be sure of: - If you get a value from an access of a typed property, it is the correct type. - If you get exceptions, you have logical errors in your code. Cheers Joe On Fri, Mar 18, 2016 at 8:39 PM, Lee Davis wrote: > Hi all, > > first up, major thanks to both Joe and Phil for putting this together. > I'm fully behind the concept of typed properties and think it would be a > great feature to have in the language. > > I have a few points I'd like to make on the current implementation being > proposed. > > 1) There's an issue with the way the __get magic method works. I understand > this has been introduced from allowing unset on typed properties (because > of the reasons Marco mentioned previously), however it's still something I > feel needs addressing: > > class Foo > { > public int $bar; > public function __get($name) > { > return 123; > } > } > > $foo = new Foo(); > unset($foo->bar); > var_dump($foo->bar); // int(123) > > $foo = new Foo(); > var_dump($foo->bar); // Fatal error: Uncaught TypeError: Typed property > Foo::$bar must not be accessed before initialisation > > I'd consider the fact that var_dump($foo->bar); produces different things > here a bug. They're both unset/uninitialised and should produce the same > result. Which in my opinion should be "int(123)", but i'll get onto that in > a bit. > > > 2) Having a guarantee that a variable is of a certain type has a ton of > benefits, mostly that you don't have to add defensive code to check its > type. As Larry pointed out, littering your code based with !is_null() is > not a nice thing to have to do. > > Unfortunately typed variables will at some point be uninitialised. you > can't really force the developer to set them (either at construct or later) > as this is something that should in my opinion always belong at > implementation and not baked in/forced at the language level. With this in > mind, handling uninitialised state will always be a thing that needs to be > done. We can't really guarantee that the property was set up properly, or > start to return a default value of said type. > > Further to this, and the fact that the ability to unset typed properties > has been reintroduced it now becomes possible to initialise a property, > have it unset and get TypeError's on subsequent access (where no __get() is > provided). So even after initialisation we can never be 100% sure a > property hasn't been destroyed and contains the type you'd expect. > > As it doesn't appear there's any way we can actually provide this > guarantee. I'd suggest removing TypeError's on access as it's superfluous. > This would then solve my first point as the magic method would be called as > expected. I know this isn't ideal, but the type check on set is still very > powerful and still provides a guarantee that you have either an > uninitialised property, or a one of a type you'd expect. > > 3) One final small point; it would be great if we could also have typed > properties outside the scope of a class. Not a show stopper by any means, > but certainly a nice to have. > > That's all. Again thanks so much for putting this together. > > Lee > > /@leedavis81 > > On Wed, Mar 16, 2016 at 4:36 PM, Phil Sturgeon > wrote: > > > Hello everyone, > > > > I have completed the draft for an RFC, to add Typed Properties. The > > patch has been written by the one and only Joe Watkins. > > > > https://wiki.php.net/rfc/typed-properties > > > > I would really appreciate constructive feedback on this RFC, with a > > few areas especially: > > > > 1. How scared are we that integers can be expanded to floats on runtime? > > > > 2. This whole temporary nullability situation, where unset properties > > will error on attempted usage if not set. Should they instead error > > after the constructor has been called if they are still not holding a > > value? > > > > 3. Weak vs Strict. Right now this is entirely strict, with no > > declare() to change mode. Reasons for this vary, from various sources, > > but include "Not sure how to implement it" and "Well people should not > > be using properties as part of their public API". > > > > Help on 3 would be appreciated. > > > > Also let's please avoid "PHP IS TURNING INTO JAVA" and the other > > rather common rhetoric. Strict Type Hinting might have been seen as a > > battleground for fans of strict and fans of weak to fight through a > > keyboard, but this RFC will not be the repeat. > > > > We'll have a nice, orderly, constructive conversation about this RFC, > > and improve the patch as you all provide feedback. > > > > Let me know what you think folks! > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > --94eb2c086cd2400582052e616957--