Using PHP, how do I define/declare getter and setter methods/functions as part of the declaration of a property in a class?
What I'm trying to do is specify the getter and setter methods as part of a property, instead of declaring separate set_propertyName($value)
and get_propertyName()
functions/methods.
What I've got:
class my_entity {
protected $is_new;
protected $eid; // entity ID for an existing entity
public function __construct($is_new = FALSE, $eid = 0) {
$this->is_new = $is_new;
if ($eid > 0) {
$this->set_eid($eid);
}
}
// setter method
public function set_eid($eid) {
$is_set = FALSE;
if (is_numeric($eid)) {
$this->eid = intval($eid);
$is_set = TRUE;
}
return $is_set;
}
}
What I want (without making $this->eid an object):
class my_entity {
protected $is_new;
// entity ID for an existing entity
protected $eid {
set: function($value) {
$is_set = FALSE;
if (is_numeric($value)) {
$this->eid = intval($value);
$is_set = TRUE;
}
return $is_set;
}, // end setter
};
public function __construct($is_new = FALSE, $eid = 0) {
$this->is_new = $is_new;
if ($eid > 0) {
$this->set_eid($eid);
}
}
// setter method/function removed
}