I am new to getters and settings, and want to start trying them. I see how I can retrieve a single property, but how can I receive retrieve properties or all of them in JSON format such as {"firstField":321, "secondField":123}. I've tried public function get(){ return $this;} and even public function getJson(){return json_encode($this);} but just get empty JSON.
PS. Is return $this; in the setter a typo or does it provide some value?
<?php
class MyClass {
private $firstField;
private $secondField;
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
public function __set($property, $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
}
return $this;
}
}
?>