# Getters/Setters and parent getter/setter access

- Thread: https://externals.io/message/56422
- Original archive: https://news-web.php.net/php.internals/56422
- Started by Clint M Priest on 2011-11-19T07:15:04+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #56422 — Clint M Priest — 2011-11-19T07:15:04+00:00

URL: https://externals.io/message/56422#56422

The RFC here: https://wiki.php.net/rfc/propertygetsetsyntax

Talks about allowing a sub-class to access a parent getter via TimePeriod::$Milliseconds or possibly parent::$Milliseconds.

Either of those methods (currently) tries to access a static property in the parent or defined class.  It would probably break existing code if we tried to make the parent:: or TimePeriod:: syntax to access the parent accessor.

Anyone have any suggestions on an alternative syntax?

I'm sure I could change it so that parent:: or TimePeriod:: from within a getter/setter would cause it to access the parent getter/setter but that would create an inconsistency within the language.

Ideas?

-Clint

---

## #56437 — Yasuo Ohgaki — 2011-11-19T21:43:34+00:00

URL: https://externals.io/message/56422#56437 · In reply to: #56422

This is not a alternate syntax suggestion, but a currently working
solution (well partial)

<?phptrait Accessors{  public function __get($name) {    if
($this->r_property[$name])      return $this->$name;    else
trigger_error("Access to read protected property");}
public function __set($name, $value) {  if ($this->w_property[$name])
   $this->$name = $value;  else     trigger_error("Access to write
protected property");}}
class OrderLine{  use Accessors;
  private $r_property = array('price'=>1, 'amount'=>1);  private
$w_property = array('price'=>1, 'amount'=>1);
  protected $price;  private $amount;
  public function getTotal() {    return $this->price * $this->amount;  }}
$line = new OrderLine;
$line->price = 20;$line->amount = 3;
echo "Total cost: ".$line->getTotal();?>

You might would like to add as a current solution.

--
Yasuo Ohgaki
yohgaki@ohgaki.net



2011/11/19 Clint M Priest <cpriest@zerocue.com>:
> The RFC here: https://wiki.php.net/rfc/propertygetsetsyntax
>
> Talks about allowing a sub-class to access a parent getter via TimePeriod::$Milliseconds or possibly parent::$Milliseconds.
>
> Either of those methods (currently) tries to access a static property in the parent or defined class.  It would probably break existing code if we tried to make the parent:: or TimePeriod:: syntax to access the parent accessor.
>
> Anyone have any suggestions on an alternative syntax?
>
> I'm sure I could change it so that parent:: or TimePeriod:: from within a getter/setter would cause it to access the parent getter/setter but that would create an inconsistency within the language.
>
> Ideas?
>
> -Clint

---

## #56438 — Yasuo Ohgaki — 2011-11-19T21:45:21+00:00

URL: https://externals.io/message/56422#56438 · In reply to: #56437

It seems gmail broke newlines :(
but you'll see the idea.

--
Yasuo Ohgaki
yohgaki@ohgaki.net



2011/11/20 Yasuo Ohgaki <yohgaki@ohgaki.net>:
> This is not a alternate syntax suggestion, but a currently working
> solution (well partial)
>
> <?phptrait Accessors{  public function __get($name) {    if
> ($this->r_property[$name])      return $this->$name;    else
> trigger_error("Access to read protected property");}
> public function __set($name, $value) {  if ($this->w_property[$name])
>   $this->$name = $value;  else     trigger_error("Access to write
> protected property");}}
> class OrderLine{  use Accessors;
>  private $r_property = array('price'=>1, 'amount'=>1);  private
> $w_property = array('price'=>1, 'amount'=>1);
>  protected $price;  private $amount;
>  public function getTotal() {    return $this->price * $this->amount;  }}
> $line = new OrderLine;
> $line->price = 20;$line->amount = 3;
> echo "Total cost: ".$line->getTotal();?>
>
> You might would like to add as a current solution.
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>
>
>
> 2011/11/19 Clint M Priest <cpriest@zerocue.com>:
> > […]

---

## #56439 — Paul Dragoonis — 2011-11-19T23:51:20+00:00

URL: https://externals.io/message/56422#56439 · In reply to: #56438

Link us to a http://gist.github.com paste please Yasou. Thanks.


> It seems gmail broke newlines :(
> but you'll see the idea.
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>
>
>
> 2011/11/20 Yasuo Ohgaki <yohgaki@ohgaki.net>:
> > […]

---

## #56440 — Yasuo Ohgaki — 2011-11-20T00:26:03+00:00

URL: https://externals.io/message/56422#56440 · In reply to: #56439

Sure. I did.

https://gist.github.com/1379592

--
Yasuo Ohgaki
yohgaki@ohgaki.net


2011/11/20 Paul Dragoonis <dragoonis@gmail.com>:
> Link us to a http://gist.github.com paste please Yasou. Thanks.
>
> 
> > […]
