Lefteris Posted June 20, 2008 Share Posted June 20, 2008 Hello all. I have had several problems with php inheritance. I am used to the way c++ deals with inheritance so I thought I could inherit constructors somehow. Seems I can't. But my newest problem is this. I will show you an example of it with the following code: class parent { private $other; public function __construct() { $this->other = new Other(); } public function doSomething() { $this->other->do(); } } class child extends parent { public function __construct() { $this->other = new Other(); } public function newFunction() { parent::doSomething(); } } When I make a new child ... and call the newFunction() I get a: Fatal error: Cannot access empty property in the line where the parent's doSomething has the other object. Which means it is empty. But why is it empty? Isn't that the same object I am instantiating at the child's constructor? I wanted to make my code a little more neat ... a little more OOP and now I think I just killed my code Quote Link to comment https://forums.phpfreaks.com/topic/111157-an-inheritance-problemquestion/ Share on other sites More sharing options...
KevinM1 Posted June 20, 2008 Share Posted June 20, 2008 Hello all. I have had several problems with php inheritance. I am used to the way c++ deals with inheritance so I thought I could inherit constructors somehow. Seems I can't. But my newest problem is this. I will show you an example of it with the following code: class parent { private $other; public function __construct() { $this->other = new Other(); } public function doSomething() { $this->other->do(); } } class child extends parent { public function __construct() { $this->other = new Other(); } public function newFunction() { parent::doSomething(); } } When I make a new child ... and call the newFunction() I get a: Fatal error: Cannot access empty property in the line where the parent's doSomething has the other object. Which means it is empty. But why is it empty? Isn't that the same object I am instantiating at the child's constructor? I wanted to make my code a little more neat ... a little more OOP and now I think I just killed my code You can inherit the parent constructor. In fact, PHP does it inherently behind-the-scenes. Try: <?php class Parent { private $other; public function __construct() { $this->other = new Other(); } public function doSomething() { $this->other->do(); } } class Child extends Parent { public function newFunction() { parent::doSomething(); } } ?> Similarly, if you want to directly invoke the parent constructor, you could always go: <?php public function __construct() { parent::__construct(); //additional code if needed } ?> Quote Link to comment https://forums.phpfreaks.com/topic/111157-an-inheritance-problemquestion/#findComment-570495 Share on other sites More sharing options...
Lefteris Posted June 21, 2008 Author Share Posted June 21, 2008 Hello and thanks for the reply. I did what you said and just used the constructor it inherited from its parent class but I still get an error at the same line, only this time it is a very different one. Call to a member function do() on a non-object in ... blah blah Still Following the example code I put it this topic. Any idea why I get this? Quote Link to comment https://forums.phpfreaks.com/topic/111157-an-inheritance-problemquestion/#findComment-571033 Share on other sites More sharing options...
keeB Posted June 21, 2008 Share Posted June 21, 2008 Snip from night: <?php class Parent { private $other; public function __construct() { $this->other = new Other(); } public function doSomething() { $this->other->do(); } } class Child extends Parent { public function newFunction() { parent::__construct(); # added, works. parent::doSomething(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/111157-an-inheritance-problemquestion/#findComment-571208 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.