kn0wl3dg3 Posted October 13, 2011 Share Posted October 13, 2011 I have these two classes: <?php class A { private $id; public function __construct() { $this->id = '11111'; } public function getProperty($prop) { return $this->$prop; } }?> <?php class B extends A { private $message = "Nope, can't see it"; public function __construct() { parent::__construct(); $this->message = "Yep, I can see it"; } }?> I tried this, but it just outputs the id (the first call) $class = new B; echo $class->getProperty('id'); echo $class->getProperty('message'); Why is that? I thought I could use a parent method on a child property.. *EDIT* Using a public or protected visibility on the message property gets me the output I expect...how come? Quote Link to comment https://forums.phpfreaks.com/topic/249063-extending-a-class-why-cant-i-access-a-parent-method-in-a-child/ Share on other sites More sharing options...
codefossa Posted October 13, 2011 Share Posted October 13, 2011 Little example that should help ya out. <?php Class A { public function __construct() { echo 'Class A<br />'; } public function say($string) { echo $string; } } Class B extends A { public function __construct() { parent::__construct(); echo 'Class B'; } } $b = new B(); $b -> say('<br />hi'); ?> Outputs Class A Class B hi Quote Link to comment https://forums.phpfreaks.com/topic/249063-extending-a-class-why-cant-i-access-a-parent-method-in-a-child/#findComment-1279109 Share on other sites More sharing options...
Psycho Posted October 13, 2011 Share Posted October 13, 2011 You need to turn on error reporting. If you did you would see the following error message: Fatal error: Cannot access private property B::$message in C:\xampp\htdocs\test\test.php on line 9 Make the property public. Quote Link to comment https://forums.phpfreaks.com/topic/249063-extending-a-class-why-cant-i-access-a-parent-method-in-a-child/#findComment-1279115 Share on other sites More sharing options...
kn0wl3dg3 Posted October 13, 2011 Author Share Posted October 13, 2011 @kira: Thanks for the reply. I'm not sure that's exactly what I'm trying to do. I get that you can call a function that was defined in class A in class B by extending class A, but take a look at my example again..I defined a function inside A that returns a value. If I use it to return a property defined in class A it works, but not if I call it to return a property from class B (unless the property is public or protected). I thought that was the point of extending.. maybe I'm missing something. Again, thanks for the reply. And sorry if I misunderstood your example @mjdamato: I did find that out, as I noted in the edit above. I'm wondering why. Any ideas? Thanks (and I do have error reporting to E_ALL..never kicked out an error) Quote Link to comment https://forums.phpfreaks.com/topic/249063-extending-a-class-why-cant-i-access-a-parent-method-in-a-child/#findComment-1279116 Share on other sites More sharing options...
KevinM1 Posted October 13, 2011 Share Posted October 13, 2011 Private hides whatever it's attached to from everything except the class in which the private member was defined. Protected hides whatever it's attached to from everything except the class in which the protected member was defined and any child classes. In other words, it's working properly. Quote Link to comment https://forums.phpfreaks.com/topic/249063-extending-a-class-why-cant-i-access-a-parent-method-in-a-child/#findComment-1279117 Share on other sites More sharing options...
kn0wl3dg3 Posted October 13, 2011 Author Share Posted October 13, 2011 In other words, it's working properly. I knew that just don't know why it is. I get the difference between visibilities. I just thought by extending Class A, I would be able to use a method from class A on Class B. But I think I get what your saying. Even though I extended A, class B still considers A to be an outside class and therefore wouldn't allow access to B's private property? I thought by extending it I could use it; that's where my confusion lies. Quote Link to comment https://forums.phpfreaks.com/topic/249063-extending-a-class-why-cant-i-access-a-parent-method-in-a-child/#findComment-1279120 Share on other sites More sharing options...
KevinM1 Posted October 13, 2011 Share Posted October 13, 2011 But I think I get what your saying. Even though I extended A, class B still considers A to be an outside class and therefore wouldn't allow access to B's private property? Bingo, although you transposed A and B (A is the parent, denying B access to its private member). Private means private, a.k.a., "Hands off my cookies (even you, Junior)!" Protected means family, but no outsiders. It's a useful distinction to have because, even if you want a class to be extended, there are rare occasions when you don't want certain data members to be accessible down the hierarchy chain. Quote Link to comment https://forums.phpfreaks.com/topic/249063-extending-a-class-why-cant-i-access-a-parent-method-in-a-child/#findComment-1279128 Share on other sites More sharing options...
kn0wl3dg3 Posted October 13, 2011 Author Share Posted October 13, 2011 Bingo, although you transposed A and B (A is the parent, denying B access to its private member). Private means private, a.k.a., "Hands off my cookies (even you, Junior)!" Protected means family, but no outsiders. It's a useful distinction to have because, even if you want a class to be extended, there are rare occasions when you don't want certain data members to be accessible down the hierarchy chain. I understand that. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/249063-extending-a-class-why-cant-i-access-a-parent-method-in-a-child/#findComment-1279130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.