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:[email protected]]
> Sent: Wednesday, October 10, 2012 6:47 PM
> To: [email protected]
> 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 <[email protected]>
> To: Leigh <[email protected]>
> Cc: Clint Priest <[email protected]>, "[email protected]" <
> [email protected]>
> 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.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php