Re: [RFC][Vote] Typed Properties

From: Date: Thu, 26 May 2016 20:00:04 +0000
Subject: Re: [RFC][Vote] Typed Properties
References: 1 2 3 4 5 6 7 8 9 10 11 12 13  Groups: php.internals 
Request: Send a blank email to internals+get-93585@lists.php.net to get a copy of this message
On 5/26/2016 9:56 PM, Rowan Collins wrote: > The current proposal (or the one which I am advocating) for that is that > you would have something like the following: > > class Person { > public \DateTime $birth; > public ?\DateTime $marriage; > public ?\DateTime $death; > public int $numSiblings; > } > > Creating an object would leave it in an invalid state: > > $me = new Person; > // accessing $me->birth or $me->numSiblings is an error in this state, > because defaulting to NULL would violate their type constraint > // accessing $me->marriage or $me->death returns NULL (because they > allow nulls) but raises E_NOTICE > > Now you can initialise the mandatory fields: > > $me->birth = new \DateTime($db_record['birth_date']); > $me->marriage = new \DateTime($db_record['marriage_date']); > $me->death = new \DateTime($db_record['death_date']); > $me->numSiblings = $db_record['num_siblings']; > // all properties can now be safely accessed > // $me->marriage and $me->death may still be null, as indicated by the > "?" in their type spec > // $me->birth is guaranteed to be a \DateTime object > > Regards, > Nothing to add here. :) PS: This needs a cast unless some special driver options are active. $me->numSiblings = (int) $db_record['num_siblings']; -- Richard "Fleshgrinder" Fussenegger

Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
« previous php.internals (#93585) next »