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
signature.asc
Description: OpenPGP digital signature
