So i am learning oop PHP. I just want to know that i have figured this out correct.
I have a class called Bicycle that has a subclass called UniCycle. My Bicycle class has a private property, and i understand that i cant accses it outside from this class.
However, in my subclass Unicycle, i can get access to the public getters/setter functions that set and gets the value from the private property. And because of that i can also from my subclass set and get the value from weight_kg.
My question: Is this a correct way to use private properties in subclasses? Or is this just a bug from overloading in php?
class Bicycle {
private $weight_kg = 0.0;
public function weight_kg() {
return $this->weight_kg . ' kg';
}
public function set_weight_kg($value) {
$this->weight_kg = floatval($value);
}
}
class UniCycle extends Bicycle {
}
echo "Set weight for Unicycle<br />";
$uni->set_weight_kg(1);
echo $uni->weight_kg() . "<br />";
echo $uni->weight_lbs() . "<br />";