Is it possible to forbid to implement interface directly? Instead of this class must implement descendant of this interface (like Traversable).
2 Answers
it may be possible to define your interface with the protected
keyword, so only classes in the same package can implement the interface.

- 2,592
- 2
- 21
- 42
Traversable is an internal interface. There are no descendants of it. It's just that when you're in PHP userland (that his where you write PHP code) you can not implement Traversable directly but only one of those two which are a Traversable, too:
But the reason is not that there is some descendant relation that you could control in PHP userspace, but only that Traversal is an internal interface not available for you in userspace.
Apart from interfaces, what you can do is to create an abstract class which can not be instantiated. You later on ask for that abstract type:
abstract class Mineable
{
// ...
}
class MineAggregate extends Mineable
{
// ...
}
class Mine extends Mineable
{
// ...
}
Those aren't interfaces, for a good reason. The Traversable example might have misguided you, it should normally not be done what's been done there. See as well:
So naturally you can build this up with interfaces as well, however any interface can be implemented:
interface Mineable {} // this interface is empty just for the taste of it
interface MineAggregate extends Mineable
{
// ...
}
interface Mine extends Mineable
{
// ...
}
I don't know if that is what you're looking for, your question was not really clear to me I must admit.
-
thanks for exhaustive answer. I`m looking for such strcucture: interface PaymentSystem{} interface Sort1PaymentSystem extends PaymentSystem{ public function check(); } interface Sort2PaymentSystem extends PaymentSystem { publick function create(); } class Payer { //$ps_handler must implement one of Sort1PaymentSystem or //Sort2PaymentSystem. Not PaymentSystem directly public function pay(PaymentSystem $ps_handler) {} } – woland Dec 26 '14 at 07:38