We have Traits. How about simply:

trait Accessors
{
    public function __get($name) {
        $fn = "get_$name";

        return method_exists($this, $fn)
            ? $this->$fn()
            : $this->_get($name);
    }

    public function __set($name, $value) {
        $fn = "get_$name";

        if (method_exists($this, $fn)) {
            $this->$fn($value);
        } else {
            $this->_set($name, $value);
        }
    }
}

The trait falls back on _get() and _set() as your __get() and __set()
equivalent methods if you need both accessors and magic.

With a standard trait like this, people would be strongly encouraged to
standardize on accessor methods - and IDE support could be implemented
easily, since accessor-methods would then be standardized.

That said, I was sad to see accessors getting rejected - it is one of the
most widely implemented PHP features... I don't think there is one
framework that does not have some variety of accessors, and I have not
personally written a library or application in the past 5 or 6 years
without adding boilerplate practically identical to the above...

- Rasmus Schultz


---------- Forwarded message ----------
From: Galen Wright-Watson <ww.ga...@gmail.com>
To: rquadl...@gmail.com
Cc: Brandon Wamboldt <bran...@brandonwamboldt.ca>, Nikita Popov <
nikita....@gmail.com>, PHP internals <internals@lists.php.net>
Date: Mon, 3 Jun 2013 12:14:28 -0700
Subject: Re: [PHP-DEV] Random Monday thought.
On Mon, Jun 3, 2013 at 10:30 AM, Richard Quadling <rquadl...@gmail.com
>wrote:

> On 3 June 2013 18:22, Brandon Wamboldt <bran...@brandonwamboldt.ca> wrote:
>
> > I think the point was that if somebody wants to extend one another
class,
> > maybe one of the SPL classes for example, they can't also extend the
base
> > class with getter/setter support so it's an incomplete solution that
will
> > frustrate many users.
> > [...]
> >
> Ah. DOH!
>
> Would having an interface that swapped the default property accessor logic
> be any better?
>

Or a trait ("Accessable", "Accessored", "Accessorable")? Is it possible to
have a trait implemented internally? Though it seems that this would still
sometimes run afoul of mixing new accessor logic with old.

Reply via email to