Re: [RFC][Vote] Typed Properties

From: Date: Fri, 27 May 2016 09:45:07 +0000
Subject: Re: [RFC][Vote] Typed Properties
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  Groups: php.internals 
Request: Send a blank email to internals+get-93603@lists.php.net to get a copy of this message
On 27.05.2016 at 10:27, Rowan Collins wrote: > On 26/05/2016 23:03, Lester Caine wrote: >> On 26/05/16 22:38, Rowan Collins wrote: >>>>> // accessing $me->marriage or $me->death returns NULL (because they >>>>>> allow nulls) but raises E_NOTICE >>>> BUT DateTime currently will not store 'null' - it returns 'now' >>>> instead. >>>> We end up having to store string or integer values because we can't >>>> store a null date :( >>> >>> That's exactly what ?DateTime is for - "either DateTime or Null", just >>> like in a database. >>> >>> In most type systems, there is no such thing as "a null date" - if it's >>> null, it's not a date, it's a null value. >> >> The exact question here then is in relation to just how one uses >> 'DateTime' in this situation? Or more accurately how one maintains the >> 'or Null' state when the type does not allow null itself? > > I don't understand the question. > > class Foo { ?DateTime $d } > $f = new Foo; > $f->d = null; > $f->d = new DateTime; > > It's either a DateTime, or it's null. ACK. However, an alternative might be: class NullDateTime extends DateTime {...} class Foo { DateTime $d } $f = new Foo; $f->d = new NullDateTime; $f->d = new DateTime; -- Christoph M. Becker

« previous php.internals (#93603) next »