> 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 <tbprogram...@gmail.com>
To: Leigh <lei...@gmail.com>
Cc: Clint Priest <cpri...@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.

Reply via email to