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