Someone
Someone

Reputation: 10575

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.

Upvotes: 1

Views: 842

Answers (4)

Sean Walsh
Sean Walsh

Reputation: 8344

You can only call a protected method from inside the class itself or any subclasses. I would recommend taking a look at the visibility entry in the PHP manual.

Upvotes: 0

user479911
user479911

Reputation:

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() {

Upvotes: 3

Shakti Singh
Shakti Singh

Reputation: 86336

Method is protected you can not call this method outside the class and the class which is inherited by this class.

Make it public if you want to call outside the class.

Upvotes: 1

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17752

Protected means that this method is available within the class and to class, that inherits this class. You should use Public if you want to call it from "outside".

Upvotes: 2

Related Questions