Search code examples
phpphp-5.3

why is this abstract class returning fatal Error in php


abstract  class MyAbstractClass{
    abstract  protected function doSomeThing();
    function threeDots(){
        return  "...";
    }
}
class MyClassA extends  MyAbstractClass{
    protected function doSomeThing(){
        $this->threeDots();
    }
}
$myclass = new MyClassA();
$myclass->doSomething();

this is the error that is being spitted out "Fatal error: Call to protected method MyClassA::doSomething() from context in test.php on line 10 ".Iam trying to know the reason for this error.


Solution

  • You have declared the function doSomething to be proteced, which means it can only be used inside parent classes, child classes or itself. You're using it outside of that.

    You can try changing

    abstract  protected function doSomeThing();
    

    into

    abstract public function doSomeThing();
    

    and

    protected function doSomeThing(){
    

    into

    public function doSomeThing() {