If I have the following PHP class example setup...
class foo {
public $x = 2;
public function getX() {
return $this->x;
}
public function setX($val) {
$this->x = $val - $this->x;
return $this;
}
}
$X = (new foo)->setX(20)->getX();
How comes I need the ->getX(); part on the end of the object initiation process in order to get 18? How come I simply can't hide the public getX() function and write...
$X = (new foo)->setX(20);
echo $X; // and show 18 without errors.
Instead it throws an error and says...
Catchable fatal error: Object of class foo could not be converted to string in C:\...
Is not $this->x refering to public $x = 2? I guess I'm a little confused why we're depending on Public function getX(). Thanks in advance for help understanding!