Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93610 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93883 invoked from network); 27 May 2016 14:12:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 May 2016 14:12:37 -0000 Authentication-Results: pb1.pair.com smtp.mail=derokorian@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=derokorian@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.41 as permitted sender) X-PHP-List-Original-Sender: derokorian@gmail.com X-Host-Fingerprint: 209.85.218.41 mail-oi0-f41.google.com Received: from [209.85.218.41] ([209.85.218.41:35015] helo=mail-oi0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A1/29-37127-4D558475 for ; Fri, 27 May 2016 10:12:36 -0400 Received: by mail-oi0-f41.google.com with SMTP id w184so168727537oiw.2 for ; Fri, 27 May 2016 07:12:36 -0700 (PDT) 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; bh=nrU7uQR2OV9zrjq69L+7j0Lth1oqEe0fTv9vyVE7A+4=; b=VB8XFkIhbrotv8ljRgKkJNCkPnpdd/udiKBfKOUuTeP/7kDBHI43SkRT3fp8xI4bFQ NGV5klE4whPNuZolSosERUKVsp1F+BUk+Darqi9o+3QZZtQjuoFTtJyD1KSeNChn49kj Pl8bq7O5s+AoByClwTCk7CO0z68mSfA9yO0CrVqCD9sQO3m9CI4Ia6j33MchZZq7voRF PJ3mCrzs2azNjzMiYMvqtLXEHpTpGtza2DGg4y5NybhyEjT9Cayn2W/lzwZ7Uja3TPfe MRcUVqMCyQh17/G1LwOSEGSYMSY89YEOULVGLkje535F1gYD5WA0+KnKtN1se2RSlPZN zChA== 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=nrU7uQR2OV9zrjq69L+7j0Lth1oqEe0fTv9vyVE7A+4=; b=MYb2IANuyx3F52Lq+xYCH9djrxXLNpU994iGJqZ7iN+coR0GViQXazu1iBN76f/gV1 Aw4duH1EvoFGIvrwkLsJZNE4TFdbnegghz64hLsxrprx4q+S7PsqbAJkDzdElXBsGF3P WkONg2z6GJg14+CU6ePrQ9y5buHn+N2o+NZQRFUDsNH5njJCEsWCwUAeer6RTpjEHt3q ga5qb/5FFgmNFnY1J3bfPHUS0DTaprxua/W/bsqigL2e8BQaGn9EQWwglTfwW4sfdCdC bZaWUUMg6n1uNWgbO6T+bAVgQ7p+YZe+zNFJtKTInP6hIVMOdBS8iy/8SwHaPQMew+Ao 3VFg== X-Gm-Message-State: ALyK8tLk5ft14dkX+5/W/AHLF116VHM0L0hW3lfuZNzhXqGKkIVUy/PRCJeCb2poCgdJBeKAnVctuy5ubMOxXQ== MIME-Version: 1.0 X-Received: by 10.202.97.69 with SMTP id v66mr8506748oib.172.1464358353940; Fri, 27 May 2016 07:12:33 -0700 (PDT) Received: by 10.157.37.57 with HTTP; Fri, 27 May 2016 07:12:33 -0700 (PDT) In-Reply-To: <57483EC9.3000501@lsces.co.uk> References: <20160525215208.034FC1A801B3@dd1730.kasserver.com> <5fd54aa0-4fdf-c1e7-eae8-765aa89c8498@fleshgrinder.com> <08963b03-8394-6d62-4e5b-393f3fcb5647@fleshgrinder.com> <9e9db70d-d72d-d93e-0c81-18c2aa228618@gmail.com> <409002d8-b5e3-5990-7358-246adc7e3cab@fleshgrinder.com> <57474A08.80807@lsces.co.uk> <6f7403b6-bf22-a12b-dfb2-43fe82983e7d@fleshgrinder.com> <57474F9C.6080104@lsces.co.uk> <1733cfce-001b-1079-e929-88f7dde0f2dd@gmail.com> <57475F27.3090306@lsces.co.uk> <34edcbd1-2d6a-cd52-7bbb-c393ab62d32d@gmail.com> <574772BA.3030305@lsces.co.uk> <5748164C.7020803@lsces.co.uk> <5156d2d3-6c7b-b5d0-fdfd-9064d8fb1d58@fleshgrinder.com> <57483EC9.3000501@lsces.co.uk> Date: Fri, 27 May 2016 08:12:33 -0600 Message-ID: To: Lester Caine Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a113d594082ba7f0533d37f05 Subject: Re: [PHP-DEV] [RFC][Vote] Typed Properties From: derokorian@gmail.com (Ryan Pallas) --001a113d594082ba7f0533d37f05 Content-Type: text/plain; charset=UTF-8 On Fri, May 27, 2016 at 6:34 AM, Lester Caine wrote: > On 27/05/16 11:25, Fleshgrinder wrote: > > On 5/27/2016 11:41 AM, Lester Caine wrote: > >> Now I am confused, but it's probably because I'm looking at a simple > >> > >> $f->d = new DateTime( $initial ); > >> > >> While you are looking at > >> > >> If ( isset($initial) ) > >> then $f->d = new DateTime( $initial ); > >> else $f->d = null; > >> > >> While the original code would have simply been > >> > >> $f->d = $initial; > >> > >> And in an ideal world, because $d has been typed as DateTime this would > >> magically run 'new DateTime($initial)' which is where I am stumbling > >> because *I* simply look at '?DateTime $d' as creating a DateTime object > >> where in fact $d is a different type of object which will then hold the > >> link to the real one. I am still looking for $d to be an object I can do > >> all of the checks on that are provided by int or DateTime as appropriate > >> ... the object needs to exist before the 'new' in order to know if you > >> need the first or second type of $initial code ... or we make typed > >> properties only work one way or the other? > > > > PHP does not automagically instantiate the DateTime object for you, this > > is still your responsibility. The type hint on the property only > > restricts what you are allowed to assign to the property, nothing else. > > If you want something to magically happen when setting a property, PHP has a way to do this called magic methods. Think about this: class Person { protected ?DateTime $death; public function setDeath($date) { if (!empty($date) && is_string($date)) { $this->death = new DateTime($date); } elseif ($date instanceof DateTime) { $this->death = $date; } // else $date is empty, person has not died } public function __set($prop, $val) { if (method_exists($this, "set_$prop")) { $this->{"set_$prop"}($val); } } public function __get($prop) { if (property_exists($this, $prop)) { return $this->$prop; } } } $p = new Person(); $p->death = "2015-06-01"; var_dump($p->death); // class DateTime#1 (3) { ... $p2 = new Person(); $p2->death = new DateTime("2016-01-02"); var_dump($p2->death); // class DateTime#1 (3) { ... $p3 = new Person(); var_dump($p3->death); // NULL, $p3 is not dead Note, all of this is possible currently except the typing of the property, you could enforce this right now in exactly this way. If however, you want an object that will provide a format function (as in your example) whether or not it was initiated with a value, then you would need to define your own class for that: class MyDateTime { protected $dt; public function __construct($date) { if (!empty($date)) { $this->dt = new DateTime($date); } } public function format($format) { if (!empty($this->dt)) { return $this->dt->format($format); } return ''; } } Now you can blindly initialize it, and call format on it, whether or not the date is provided. This is all controllable in userland, there is no reason to make the language do it for you, as its purely a design decision. --001a113d594082ba7f0533d37f05--