|
Posted by Chung Leong on 11/27/05 01:35
> Interfaces are a kind of substitute for multiple inheritance.
Multiple inheritance is often used to implement interface in C++ true
enough. Interface in PHP would be a rather lousy subsitute for multiple
inheritance though, since you're basically inheriting classes that do
nothing.
Interface give you a quick way to check at run-time if an object has a
given set of methods. Other than that, I don't see much use for it.
The iterator example actually illustrates the problem with interface in
PHP: an object and its interfaces are one and the same. To loop through
a collection, the object has to remember the current index. Thus a
seemingly read-only operation ends up causing state changes. Unless
foreach() clone an object (which I don't it does), bad things would
happen when a second iteration is initiated before the first one
completes.
Navigation:
[Reply to this message]
|