RE: [PHP-DEV] [RFC] Propety Accessors v1.1

From: Date: Thu, 11 Oct 2012 00:06:16 +0000
Subject: RE: [PHP-DEV] [RFC] Propety Accessors v1.1
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-63329@lists.php.net to get a copy of this message
Jazzer's example was extending an accessor and her statement about no way to stop the developer from doing what she did there without read-only is correct. There are other, more verbose and less simple ways to accomplish read-only and write-only (preventing sub-classes from defining a getter, etc, namely through the use of final) but none of them are as simple and easily readable as public read-only $hours { ... } > -----Original Message----- > From: Rasmus Schultz [mailto:rasmus@mindplay.dk] > Sent: Wednesday, October 10, 2012 6:47 PM > To: internals@lists.php.net > Subject: Re: [PHP-DEV] [RFC] Propety Accessors v1.1 > > > There's no way to stop the developer from doing that without read-only. > > Yes, there is - I don't even know why would write it that way - doesn't seem to make > much sense. > > What you probably should be doing, is this: > > class A { > private $seconds = 3600; > > public $hours { > get() { return $this->seconds / 3600 }; > } > } > > Keep your field private - now try extending this with a write-accessor. > > I think that read-only is really almost merely a "pseudonym" for "read-only > accessor for a private field" - what you're really trying to > do, is protect the field behind the accessor, not the accessor itself. > > In the same way, write-only is practically synonymous with "write-only accessor for a > private field" - to some extend (at least) the > point of having accessors to begin with, is to protect the underlying value(s) from > unauthorized or incorrect use. > > You can relax your read-only or write-only accessors by declaring the backing field(s) > protected - this would be the equivalent of > declaring a read-only accessor that you are permitted to extend with a write-accessor if you > need to... > > > ---------- Forwarded message ---------- > From: Jazzer Dane <tbprogrammer@gmail.com> > To: Leigh <leight@gmail.com> > Cc: Clint Priest <cpriest@zerocue.com>, "internals@lists.php.net" < > internals@lists.php.net> > Date: Tue, 9 Oct 2012 19:33:20 -0700 > Subject: Re: [PHP-DEV] [RFC] Propety Accessors v1.1 > > > class A { > > > public $seconds = 3600; > > > > > > public $hours { > > > get() { return $this->seconds / 3600 }; > > > } > > > } > > > > > > class B extends A { > > > public $hours { // Maintains 'get' from class A > > > set($value) { $this->seconds = $value; } > > > } > > > } > > > > > ^There's no way to stop the developer from doing that without read-only..

« previous php.internals (#63329) next »