Re: Getters/Setters and parent getter/setter access
| From: | Yasuo Ohgaki | Date: | Sat, 19 Nov 2011 21:43:34 +0000 |
| Subject: | Re: Getters/Setters and parent getter/setter access | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-56437@lists.php.net to get a copy of this message | ||
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
>