Reputation: 10575
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
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
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
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
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