Completely new to OOP programming, so sorry if the answer is obvious. I've seen many YouTube tutorials where the code is as following:
class myClass{
private $myVar;
function getVar(){
return $this->myVar;
}
}
myObject = new myClass();
print myObject->getVar();
but why is it preferable to make the variable private and then access it through a function, rather than just using myObject->myVar
?
A few benefits of private variables:
You may also see here.
Was this helpful?