Problem with extending and abstract class (SOLVED)

Okay I want to start off saying I solved this problem as I was in the process of signing up but decided to post it in case it helps someone else. If this is a violation go ahead and delete the thread.

Okay so my problem was I had an abstract base class (lets call this A) that had a defined function that was calling an abstract function which was defined in a class that extended it (class B). The logic defined in class A was universal but the abstract method defined in class B was situational (will be different in class C which also extends class A). I was getting an error saying that class A could not call an abstract method when I initiated and instance of class B. Some sample code has been provided showing the issue and the fix

Causes an error
[php]
abstract class A
{
public function call1() {
echo ‘Running from inside call1
’;
self::call2(); // This line was causing an error
}

protected abstract function call2();

}

class B extends A
{
public function call2() {
echo ‘Running from inside call2
’;
}
}

$b = new B;
$b->call1();
[/php]

The error: Fatal error: Cannot call abstract method A::call2() in C:…\classtest.php on line 7

The fix was pretty easy, I was using the self:: accessor which calls the class defined method instead of the the instance defined (So since the logic is in class A, it acts as if you had an instance of class A calling it instead of class B). I made the switch to use $this-> and it works like a charm

[php]
abstract class A
{
public function call1() {
echo ‘Running from inside call1
’;
$this->call2(); // This is the fixed line
}

protected abstract function call2();

}

class B extends A
{
public function call2() {
echo ‘Running from inside call2
’;
}
}

$b = new B;
$b->call1();
[/php]

This seems pretty obvious to me now, but it did not become so until I was in the middle of writing a simple test case to show since I did not want to paste several hundred lines of code to show mine. I hope this may help someone, even if they just read this before they attempt to do the same thing I have done and know this issue before they begin :slight_smile:

Here is a silly example of abstract class & functions, but I think this will clarify it even more?

[php]<?php
/* This is an abstract class that depends on other classes for values /
/
and is overridden by the other classes. */
abstract class Animal {
private $pet = NULL;
public function type_of_pet() {
$this->pet = "The kind of pet is a " . $this->kind() . “
\n”;
$this->pet .= "The breed of the pet is a " . $this->breed() . “
\n”;
return $this->pet;
}

abstract function kind();
abstract function breed();

}

/* The class could stand on its own */
class Dog extends Animal {

/* This implements the abstract function kind() in Animal */
public function kind() {
    return "Dog";
}
 /* This implements the abstract function breed in Animal */
public function breed() {
    return "German Shepard";
}

}

$myPet = new Dog();
echo $myPet->type_of_pet();
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service