Re: Property Accessors 1.2 for Final Review before Vote
| From: | Steve Clay | Date: | Wed, 02 Jan 2013 22:18:26 +0000 |
| Subject: | Re: Property Accessors 1.2 for Final Review before Vote | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-64488@lists.php.net to get a copy of this message | ||
On 1/2/13 4:18 PM, Clint Priest wrote:
Omitting isset/unset has the same effect as declaring it without a body. This is described in the RFC under Automatic Implementations with this line: Note that isset/unset implementations will always be provided if they are not defined or if they are explicitly auto-defined (as above).I think the RFC could make this clearer: "isset & unset are always provided with the default implementations unless the author provides his/her own." Looking closer at the default implementations of isset/unset, I'm worried these could lead to confusion. Consider this code: class TimePeriod {
private $Seconds = 3600;
public $Hours {
get { return $this->Seconds / 3600; }
set { $this->Seconds = $value; }
}
}
The RFC's default implementation is always bound to the shadow property, so here's what you really get:
class TimePeriod {
private $Seconds = 3600;
public $Hours {
get { return $this->Seconds / 3600; }
set { $this->Seconds = $value; }
// auto-generated
isset { return $this->Hours != NULL; }
unset { $this->Hours = NULL; }
}
}
Note the resulting behavior:
$t = new TimePeriod;
$t->Hours; // 1
isset($t->Hours); // false !?
unset($t->Hours);
$t->Hours; // still 1
Effectively, authors who don't base their getters/setters on the shadowed property must be urged to create their own isset/unset because the default ones would be useless. I'm not crazy about this.
I'd prefer these default implementations:
isset { return $this->__getHours() != NULL; }
unset { $this->__setHours(NULL); }
$t = new TimePeriod;
$t->Hours; // 1
isset($t->Hours); // true
unset($t->Hours);
$t->Hours; // null
isset($t->Hours); // false
Note these also work as expected when using the default get/set implementations. Of, course, my implementations don't actually *work* because you can't call an accessor from an accessor...
Steve Clay
--
http://www.mrclay.org/