Re: [RFC] Propety Accessors v1.1

From: Date: Wed, 10 Oct 2012 02:33:20 +0000
Subject: Re: [RFC] Propety Accessors v1.1
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-63310@lists.php.net to get a copy of this message
I think Leigh brings up some important flaws to in the current RFC. What Leigh is asking for does not appear to be possible, and in my opinion, it should be. I also agree with Rasmus, to a certain extent. By putting only a getter/setter, the developer essentially sets the property as read or write only. At first glance, there is no need for the read-only or write-only keyword. Except... subclassing may or may not be an issue. By setting a property to read-only, it ensures that 'set' can never be set by a subclass. Unless I redefine the entire property... or is that not even possible? *The RFC isn't very clear but it appears that there is no way to redefine the entire property, as if you define it in the subclass with only get, then it will take set, isset, and unset from the parent class, correct? * Though, that can be outright stopped by making the property final. But then there is no point in having read/write-only. From what I can tell, read-only becomes useful if I extend the class and want to partially modify the property. Example: > 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. Also, if the property is public, what if an outside class tries to do this: > class A { > public $seconds = 3600; > > public $hours { > get() { return $this->seconds / 3600 }; > } > } > > $object = new A(); > $object->hours = 100; > What happens then? And similarly, how do we set a public property as a property accessor, hmm? class A { > public $hours = 1; > } > > $seconds = 20; > > $object = new A(); > $object->hours = { // Does this work? > get() { return $seconds; } > }; > There's definitely still some questions to answer before this RFC is ready. On Tue, Oct 9, 2012 at 10:19 AM, Leigh <leight@gmail.com> wrote: > > RFC Document: > https://wiki.php.net/rfc/propertygetsetsyntax-as-implemented > > public $property { > set { $this->property = ($this->property*2)+$value } > get; > } > > How do I reference the property being set from within the function? The way > I have done it in the example will cause recursion? How can I assign to > "self"? > > How do I set the default value of $property when the object is created? > Surely I don't have to reverse the set accessors logic and set the inverse > in __construct? >

« previous php.internals (#63310) next »