Jump to content

Recommended Posts

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  :P

Link to comment
https://forums.phpfreaks.com/topic/111157-an-inheritance-problemquestion/
Share on other sites

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  :P

 

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
   }
?>

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?

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();
      }
   }
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.