Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93912 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25477 invoked from network); 12 Jun 2016 15:16:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jun 2016 15:16:52 -0000 Authentication-Results: pb1.pair.com smtp.mail=lester@lsces.co.uk; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=lester@lsces.co.uk; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lsces.co.uk from 217.147.176.204 cause and error) X-PHP-List-Original-Sender: lester@lsces.co.uk X-Host-Fingerprint: 217.147.176.204 mail4.serversure.net Linux 2.6 Received: from [217.147.176.204] ([217.147.176.204:37109] helo=mail4.serversure.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2D/41-12403-2EC7D575 for ; Sun, 12 Jun 2016 11:16:51 -0400 Received: (qmail 26826 invoked by uid 89); 12 Jun 2016 15:16:47 -0000 Received: by simscan 1.3.1 ppid: 26820, pid: 26823, t: 0.0841s scanners: attach: 1.3.1 clamav: 0.96/m:52/d:10677 Received: from unknown (HELO ?10.0.0.7?) (lester@rainbowdigitalmedia.org.uk@81.138.11.136) by mail4.serversure.net with ESMTPA; 12 Jun 2016 15:16:47 -0000 To: internals@lists.php.net References: Message-ID: <575D7CDF.8030706@lsces.co.uk> Date: Sun, 12 Jun 2016 16:16:47 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC][Vote] Typed Properties From: lester@lsces.co.uk (Lester Caine) On 12/06/16 14:03, Rasmus Schultz wrote: > If I had to implement type-checked properties in plain PHP, I would do > something like this... > > /** > * @property int $price > */ > class Product > { > private $_price; > > public function __set($name, $value) { > if ($name === "price") { > if (!is_int($value)) { > throw new UnexpectedValueException("..."); > } > $this->_price = $price; $this->_price = $value; > return; > } > throw new RuntimeException("undefined property {$name}"); > } > > public function __get($name) { > return $this->{"_{$name}"}; > } > } I'm with you on not totally understanding the internals, but ... var $_price; Added code to handle the access restrictions, so checks have to be made on just how $this->_price is accessed. In my book, the __set function validates range as well as checking so value is of the right type and any range error can be handled at the right point. However if you add 'int' to the private then this now adds a check to see if there IS a type restriction on the 'var' in addition to access restriction. Checks for 'strict' come in here as well? But I still see a lot more cases where the 'is_int' checks are needed in the normal flow prior to any ACTUAL assignment to the typed property. SO it makes a lot more sense to me that '$this->_price = $value;' either does the job of validation fully, or does not do it at all? And there should be no distinction between $this->_price = $value; and $this->params_array[_price] = $value; It is exactly the same variable? -- Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk