Jump to content

Inheritance Issue


Eric_Ryk

Recommended Posts

I have a parent class that looks like this:
[code]abstract class P_Part
{
  private $parts;
  public function __construct()
  {
      $this -> parts = array();
  }
  public function get_parts()
  {
      return $parts;
  }
}[/code]

And a child class that looks like this:
[code]class C_Part extends P_Part
{
  public function __construct()
  {
      parent::__construct();
  }
  public function asdf()
  {
      $this -> parts[] = 5;
      $this -> parts[] = 6;
  }
}[/code]

Then I do something like the following:
[code]$c_part = new C_Part();
$c_part -> asdf();
var_dump($c_part -> get_parts());[/code]

I get an empty array.
Link to comment
https://forums.phpfreaks.com/topic/30690-inheritance-issue/
Share on other sites

[quote author=448191 link=topic=118659.msg485091#msg485091 date=1166134806]
Duh. It's because the parents' $parts is private. It's not the same $parts, as the parents' $parts is unavailable to the childs $parts.
[/quote]Gah, thanks for some reason I'm still in the habit of private instead of protected.
Link to comment
https://forums.phpfreaks.com/topic/30690-inheritance-issue/#findComment-141413
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.