开发者

why is this abstract class returning fatal Error in php

abstract  class MyAbstractClass{
    abstract  protected 开发者_JAVA百科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.


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


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".


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜