Abstract private functions

In the following code, PHP will be unhappy that customMethod () is private. Why is this so? Is visibility defined where something is declared, not defined?

If I wanted customMethod to be only visible to a code template in the Template class and not allow it to be redefined, could I just make it secure and final?

template.php:

abstract class Template() { abstract private function customMethod(); public function commonMethod() { $this->customMethod(); } } 

CustomA.php:

 class CustomA extends Template { private function customMethod() { blah... } } 

main.php

 ... $object = new CustomA(); $object->commonMethod(); .. 
+22
inheritance php abstraction
Oct 29 '11 at 1:30 p.m.
source share
2 answers

Abstract methods cannot be private, because by definition they must be implemented by a derived class. If you do not want it to be public , it must be protected , which means that it can be seen by derived classes, but no one else.

The PHP manual on abstract classes shows you examples of using protected in this way.

+41
Oct 29 '11 at 13:37
source share

The abstract method is open or protected. It's necessary.

+1
Oct 29 '11 at 13:33
source share



All Articles