creomire Posted July 18, 2007 Share Posted July 18, 2007 Hi, I am extending classes but having a slight problem. I have a variable in the parent class that is set by a parent object, but the child class sees the variable as null. Here's a simplified version of the code: class parent { var $config = ""; function initiate() { $this->config = "hello world"; } } class child extends parent { function display() { $content = $this->config; return $content; } } $parent = new parent; $child = new child; $parent->initiate(); $content = $child->display(); echo $content; Any help appreciated. Quote Link to comment Share on other sites More sharing options...
phpknight Posted July 18, 2007 Share Posted July 18, 2007 I've never used a class in PHP, only C++. However, from looking at the code, it looks like parent and child are separate objects. Don't you have to call initiate() for the child object? Just because the names are $parent and $child does not mean anything. It is the object that matters, and they are separate instances. Quote Link to comment Share on other sites More sharing options...
KrisNz Posted July 18, 2007 Share Posted July 18, 2007 You don't need to make an instance of parent, since child extends parent, it can do everything that parent can do. In your code the $config variable is only a property of the $parent instance you've created. the $child instance has no knowledge of the $parent instance. That probably sounds confusing, perhaps someone else can explain it more eloquently. But try this... $child = new child; $child->initiate(); $child->display(); Quote Link to comment Share on other sites More sharing options...
phpknight Posted July 18, 2007 Share Posted July 18, 2007 Right, KrisNz. I think we are saying the same thing in different ways. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 try class parent { var $config = ""; function initiate() { $this->config = "hello world"; } } class child extends parent { function display() { $content = $this->config; return $content; } } //$parent = new parent; //removed $child = new child; //$parent->initiate(); //removed $child->initiate(); $content = $child->display(); echo $content; EDIT also this should be in the OOP section! Quote Link to comment Share on other sites More sharing options...
creomire Posted July 18, 2007 Author Share Posted July 18, 2007 try class parent { var $config = ""; function initiate() { $this->config = "hello world"; } } class child extends parent { function display() { $content = $this->config; return $content; } } //$parent = new parent; //removed $child = new child; //$parent->initiate(); //removed $child->initiate(); $content = $child->display(); echo $content; EDIT also this should be in the OOP section! Surely that is wrong. A class cannot extend another unless they have both been declared. In your example, the parent class has not been declared, and therefore child cannot be an extension of it. Quote Link to comment Share on other sites More sharing options...
creomire Posted July 18, 2007 Author Share Posted July 18, 2007 You don't need to make an instance of parent, since child extends parent, it can do everything that parent can do. In your code the $config variable is only a property of the $parent instance you've created. the $child instance has no knowledge of the $parent instance. That probably sounds confusing, perhaps someone else can explain it more eloquently. But try this... $child = new child; $child->initiate(); $child->display(); I'll test this. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 class child extends parent tells the system to include the parent see and old example of mine extends Quote Link to comment Share on other sites More sharing options...
creomire Posted July 18, 2007 Author Share Posted July 18, 2007 class child extends parent tells the system to include the parent see and old example of mine extends It doesn't work like that, you need to declare the parent class first. Quote Link to comment Share on other sites More sharing options...
creomire Posted July 18, 2007 Author Share Posted July 18, 2007 You don't need to make an instance of parent, since child extends parent, it can do everything that parent can do. In your code the $config variable is only a property of the $parent instance you've created. the $child instance has no knowledge of the $parent instance. That probably sounds confusing, perhaps someone else can explain it more eloquently. But try this... $child = new child; $child->initiate(); $child->display(); I got a workaround with this, many thanks. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 It doesn't work like that, you need to declare the parent class first. Tried and tested code Note: the parent to myparent etc <?php class myparent { var $config = ""; function initiate() { $this->config = "hello world"; } } class mychild extends myparent { function display() { $content = $this->config; return $content; } } //$parent = new parent; //removed $child = new mychild; //$parent->initiate(); //removed $child->initiate(); $content = $child->display(); echo $content; ?> Quote Link to comment Share on other sites More sharing options...
creomire Posted July 18, 2007 Author Share Posted July 18, 2007 It doesn't work like that, you need to declare the parent class first. Tried and tested code Note: the parent to myparent etc <?php class myparent { var $config = ""; function initiate() { $this->config = "hello world"; } } class mychild extends myparent { function display() { $content = $this->config; return $content; } } //$parent = new parent; //removed $child = new mychild; //$parent->initiate(); //removed $child->initiate(); $content = $child->display(); echo $content; ?> Whoops! My bad. I sincerely apologise... that must have been annoying there. The parent class doesn't have to have an instance made, just declared in the php script before the child. Tell me, if I did create an instance of $parent, and also an instance of $child... and then only used the $child operator (seeing as it does everything anyway), would this use more memory than calling $child alone? Is there an advantage at all? Many thanks for the reply. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 I had basically the same question (see link) and user:448191's reply was very cool but simply put if $parent took up (100kb) and $child took (excluding the extension 50kb) 150kb, then if you declare them both it will use up 250kb BUT if you needed 2 instance but only the core ($parent) for 1 instance then you could have 1 parent and 1 child.. i hope that makes sense! OK heads hurting now! Quote Link to comment Share on other sites More sharing options...
creomire Posted July 18, 2007 Author Share Posted July 18, 2007 I had basically the same question (see link) and user:448191's reply was very cool but simply put if $parent took up (100kb) and $child took (excluding the extension 50kb) 150kb, then if you declare them both it will use up 250kb BUT if you needed 2 instance but only the core ($parent) for 1 instance then you could have 1 parent and 1 child.. i hope that makes sense! OK heads hurting now! Ahh! So only declare one instance if possible. Makes sense. Thanks a ton. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 Yep.. but if you do need to declare twice then think if you need all the extended classes, of course you can have an extension of an extension of an extension of an extension etc so you can declare in the middle.. its all about planning ahead! EDIT: if this topic is closed please click Topic Solved (bottom left) Quote Link to comment 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.