How to enforce *optionally used* method signatures

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Forum Newbie
Posts: 9
Joined: Thu Feb 12, 2015 3:45 pm

How to enforce *optionally used* method signatures

Post by phpRob »

I made this thread title as specific as I could but there are these additional constraints related to the question...
[*] I create unique/customized classes that you can think of as type "C" that extend a class "B" which in turn extends "A".
[*] It does not make sense for class "A" to use the methods which I want to enforce signatures for; it does offer much utility to "B" by "B" extending "A" ("B" must extend "A" due to good OOP design).
[*] Class "B" wants the methods to be protected and optional for all customized "C" classes (this rules out PHP Interfaces and also protected abstract functions because the methods are not to be public and are to be optionally used by "C").
[*] The methods whose signatures I wish to enforce consistently across all "C"-type classes (each having their own unique properties and thus creating the need for the existence of these classes) needs to be visible only to the class (not public).
[*] Each object of the "C" class variety must have its own unique implementation of the methods whose signatures I want enforced (this rules out the use of traits).
[*] How many methods am I talking about? just eight; but again, any class of the "C" variety can make use of 0-8 of these methods; it is consistency of their signatures I am concerned about.
If anyone has an idea how to achieve this: WOW. Because I'm thinking that I need to relax the desire to have the methods be optional and just create empty shell implementations for them in the "C"-type classes wherever they are really not needed, having made the methods abstract functions in class "B". Thankfully class "B" only needs to be extended and so I can accordingly as required make it an abstract class. But is there a better way?
User avatar
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to enforce *optionally used* method signatures

Post by Christopher »

I am confused as to exactly what you are trying to do. Is it that you want C to inherit a different set of methods than B inherits from A. Or are you trying to have a different mix of methods available in your C class depending the situation. Either way, I think you will need to use __call() to implement this. In the first case, you can't overload from protected to private. In the second, __call() is the only way to created a dynamic interface.
Post Reply