0

I need some help. This code doesn't mean to do anything I was just trying to learn how these inheriting functions work.

So both parents and child class have $wheels variable and set to private (It doesn't make sense but I was just playing around with the code).

A method is used to print the number of wheels of the Instance. This method is not overridden in the child class. Parent and child objects were created and wheel_details() were invoked using each object.

However, when invoked using the child object, the method doesn't use the child object's private var. Instead it prints the parent object's var.

How and why do you think it accesses the parent class's private var instead of its own private var.

Appreciate an insight on this. TIA

class Bicycle {

    private $wheels = 2;


    public function wheel_details(){
        return " It has {$this->wheels} wheels. Invoked by ". get_class($this) ;
    }

}

class Unicycle extends Bicycle {

    private $wheels = 1;

}

$b1 =  new Bicycle;
$b2 =  new Unicycle;


echo "Bicycle1 wheels ". $b1->wheel_details(). " <br />";
echo "Bicycle2 wheels ". $b2->wheel_details(). " <br />";

/*Output
=======
Bicycle1 wheels It has 2 wheels. Invoked by Bicycle 
Bicycle2 wheels It has 2 wheels. Invoked by Unicycle
*/

Desper
  • 83
  • 2
  • 11
  • 1
    If you use a good ide for php (phpstorm is best in my experience), it does a good job of showing you that stuff. Look at this screenshot of your situation: https://i.imgur.com/hyzhWjl.png – farhang May 20 '19 at 07:57
  • Put that wheel_details function in a trait and it will work as expected. – Progrock May 20 '19 at 08:27
  • Possible duplicate of [What is the difference between public, private, and protected?](https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected) – yivi May 20 '19 at 11:22

1 Answers1

2

This is by design:

Members declared as private may only be accessed by the class that defines the member.

If you want to override the value from a child class, use protected instead.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • Indeed mate protected works. But how can a child instance have access to the parent's private variable? The method is called using a child object – Desper May 20 '19 at 07:51
  • 1
    Because method is called in __context__ of parent class. – u_mulder May 20 '19 at 07:54
  • 1
    It actually does not have access to it. But that wheel_details function does have access to the private $wheels=2, so it uses it – farhang May 20 '19 at 07:55
  • 1
    @Desper : Private variable are only access by only with the class where it is write. Child can't access parents private things (Yes your understood `private things` correctly) , That's why access modifier is created. read in [article1](https://www.studytonight.com/php/php-access-modifiers) – Drone May 20 '19 at 07:56
  • @Farhang & u_mulder So, since the method is invoked in the context of the parent class, it chooses to ignore the child class's private var? – Desper May 20 '19 at 08:33
  • 1
    It does not ignore it - it doesn't have access to it. If you'd copied the method into the inherited class, it'd access the variable local to that class. The member declared as `private` can _only_ be accessed by code running in that exact class. Not a parent or a child class. – MatsLindh May 20 '19 at 11:26