0

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 />";
  • 3
    "correct" is largely a matter of opinion. And this has nothing to do with overloading, because you aren't overloading anything - your unicycle class doesn't contain any overloads. If you've created those getters and setters and made them public, then any other code, not just a subclass, can access them. If they're there to be used, and that's what you intended, then use them - no problem. – ADyson Dec 18 '20 at 10:58
  • 2
    But just to note that you can also make a property "protected" instead of "private", which means it's accessible from the originating class, and from any subclasses, but not to other code outside that. So it's sort of in between public and private. Maybe that would suit your situation. If not, it's at least useful to know about. – ADyson Dec 18 '20 at 10:58
  • 1
    They're inheritance modifiers, not visibility modifiers. But since the methods share the same class scope, they're largely unaffected. Also, conceptually, there is little point in treating values aquired/fed from outside as private. – mario Dec 18 '20 at 11:02
  • 2
    Also, just my opinion on this, but your weight_kg getter is problematic, because it returns the value formatted with a string attached to it. That's nice for presentational convenience, but it means that code using your class can't use the actual weight value, e.g. if it needed it for some calculations, or to display it in a different way without the "kg" text. So arguably your concept here is a bit flawed anyway. There should really also be a way to retrieve the original value, IMHO. – ADyson Dec 18 '20 at 11:04
  • 1
    @mario I'm pretty sure PHP calls them visibility modifiers, not inheritance modifiers? https://www.php.net/manual/en/language.oop5.visibility.php . But otherwise I agree with your comment. – ADyson Dec 18 '20 at 11:10
  • 2
    Two more cents: A unicycle being a subclass of a bicycle seems… weird? Can a unicycle do everything a bicycle can *and more*? Does it make sense to use a unicycle everywhere a bicycle is expected? I'd say it'd make the most sense to have an abstract base class `PedalledVehicle`, with `Unicycle` and `Bicycle` as direct descendants of it. – deceze Dec 18 '20 at 11:15
  • I hadn't even stopped to think of that, but deceze is right. In terms of the real world objects you seem to be trying to represent, your inheritance doesn't make sense. To take deceze's suggestion further (by way of trying to clarify the concept involved), you could potentially have another abstract class `Vehicle` from which `PedalledVehicle` inherits, containing properties common to all types of vehicle. And there could be other classes which also inherit from that, to represent other categories, e.g. `MotorisedVehicle`, `AnimalPulledVehicle` or whatever. Do you see? – ADyson Dec 18 '20 at 11:21
  • @ADyson True. But I feel that's somewhat of a misnomer, and sometimes prompting such questions. (In my mind, private/protected in PHP really simulate C++, where they were originally intended as compiler optimization, e.g. omitted properties from header files and the object vtable.) – mario Dec 18 '20 at 11:26
  • 1
    @mario ah, it's just a matter of experience then. You see them like that, whereas I see them as being similar to the concepts in C# where they're usually known as access modifiers - basically a way of protecting the integrity of your class's data & logic where needed. The way these keywords work is very similar (if not identical) in PHP. But I was more talking about terminology - you may think of them as "inheritance" modifiers in your mind, but that's not useful for the OP if they are searching for info about it online. Shared understanding of terminology is a very important thing in coding. – ADyson Dec 18 '20 at 11:28

0 Answers0