There is an interface

interface TestInterface { public function test(); public function test2(); } 

There are several parent classes

 class ParentClass1 implements TestInterface { public $child = false; public function test() { if (!$this->child) { return 'fail'; } echo 'test1'; } public function test2() { echo 'test2'; } } class ParentClass2 implements TestInterface { public $child = false; public function test() { if (!$this->child) { return 'fail'; } echo 'test2'; } public function test2() { echo 'test2'; } } 

And there are randomly inherited classes

 class ChildClass1 extends ParentClass1 { public $child = 1; } class ChildClass2 extends ParentClass2 { public $child = 2; } 

The parent class itself can be created (not abstract), and the test2 method can be called. To allow the test method to be performed only for children, you have to insert this crutch into each parent class.

 if (!$this->child) { return 'fail'; } 

In the application of course, such classes are much more than 2, you have to repeat the same crutch in many classes. There was a thought to make a general abstraction for all parent classes, implement the __call method in it, make protected methods, and catch method calls. But php swears (because the interface, which means only public). Any ideas on how to catch method calls in another way?

  • one
    it is somewhat strange to support the interface and prohibit calling methods on it. - teran
  • Probably you have to make the parent classes abstract and the allowed method static. Does not quite fit into the structure of the application, but oh well - dev_null

0