Help - Inheritance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaeru
Forum Commoner
Posts: 28
Joined: Sun Mar 07, 2010 5:22 pm

Help - Inheritance

Post by michaeru »

Good day,

I have two classes, one inherits (extends) the other class.

Code: Select all

class A {

  public function __construct {

    $this->do_this();
    $this->do_that();

  }

  protected function do_this()  {
    ...
  }

  protected function do_that() {
    ...
  }

}

Code: Select all

if(!class_exists('A')) {

  require_once('A.class.php');

}

class B extends A {

  public function __construct() {

    $this->do_those();
    $this->do_this();
    $this->do_that();

  }

  private function do_those() {
    ...
  }

}

$trial = new B();
Now when I try the class, I get something like this.

[text]Fatal error: Call to private method A::do_this() from context 'B' in C:\xampp\htdocs\B.class.php on line 12[/text]

Protected methods are available to subclasses, right?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help - Inheritance

Post by Christopher »

Yes, but the error message says it is declared private. Are you sure you are looking at the file that is actually being included?
(#10850)
Post Reply