atitthaker Posted August 16, 2006 Share Posted August 16, 2006 I have the code as given below:<?phpclass ABC { function printValue() { print("This value shall be printed" . $this->x); }}class BCD extends ABC { private $x=10;}$bcd=new BCD;$bcd->printValue();?>But when I try to access printValue function from the paren class it gives me error...[color=red]Fatal error: Cannot access private property BCD::$x in /var/www/html/trainingweb/modules/test/TestAtitProblem.php on line 5[/color]As the public method of ABC would be extended to BCD, it shall have access to its own member.Plz help... Quote Link to comment https://forums.phpfreaks.com/topic/17706-problem-with-derived-class/ Share on other sites More sharing options...
DocSeuss Posted August 16, 2006 Share Posted August 16, 2006 This is much the same answer as to your other question variables declared as private are not available to any other class....they are not inherited. Quote Link to comment https://forums.phpfreaks.com/topic/17706-problem-with-derived-class/#findComment-75520 Share on other sites More sharing options...
atitthaker Posted August 16, 2006 Author Share Posted August 16, 2006 But as BCD is extending ABC so it shall not have access to *public* method of ABC and as I am instantiating object of class BCD it can access private member through ABC's (but now extended in BCD) access method.Isn't it? ??? Quote Link to comment https://forums.phpfreaks.com/topic/17706-problem-with-derived-class/#findComment-75522 Share on other sites More sharing options...
DocSeuss Posted August 16, 2006 Share Posted August 16, 2006 My host is still running php 4 so I can't personally test but I'm pretty sure if you change it protected or public it would work. Quote Link to comment https://forums.phpfreaks.com/topic/17706-problem-with-derived-class/#findComment-75527 Share on other sites More sharing options...
neta1o Posted December 1, 2010 Share Posted December 1, 2010 When referring to $this->x in ABC the x variable has to be local to that class, in your example it is not. x is local to BCD since it is declared as private. To fix this, change private $x=10; to public or protected (as mentioned above) Quote Link to comment https://forums.phpfreaks.com/topic/17706-problem-with-derived-class/#findComment-1141860 Share on other sites More sharing options...
KevinM1 Posted December 1, 2010 Share Posted December 1, 2010 Gah, didn't see the OP date. @neta1o, don't resurrect 4 year old threads. Quote Link to comment https://forums.phpfreaks.com/topic/17706-problem-with-derived-class/#findComment-1141916 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.