I have a base class which uses php magic methods __get and __set to be able to modify private attributes in the extended class. Then I built setter getter functions for the relevant private attributes (similar to what is found here http://www.beaconfire-red.com/epic-stuff/better-getters-and-setters-php )
So my child class will look as follows:
class User extends BaseObject {
public $id = -1;
private $_status = "";
function __construct($members = array()) {
parent::__construct($members);
}
//Setter/Getter
public function status($value = null) {
if($value) {
$this->_status = $value;
} else {
return $this->_status;
}
}
Now when I serialize this object which is a JsonSerialize method in the base class, the serialization will only pick up public attributes from the child class (ie "Id") but it won't pick up the private attributes (ie "_status") This is the serialization function:
public function jsonSerialize() {
$json = array();
foreach($this as $key => $value) {
$json[$key] = $value;
}
return $json;
}
Is there any way the above method in the base class can identify all Getters in the child class so that they can be included in the serialization? In other words I want the serialization to include both "id" and "status"
I realize I could get all methods on the class and use some kind of naming convention to identify the getter/setter but i specifically need to keep the getter/setter name the same as the attribute name, ie _status MUST have a getter setter called status() so is there any other way to identify these specific functions?