On Wed, 11 Mar 2020 at 14:18, Mike Schinkel <m...@newclarity.net> wrote:

>
> IOW, if interfaces cannot enforce the requirements of the "contract" why
> have interfaces?  Why not just use method_exisits() when we need to call a
> prescribed method?
>


Interfaces, at least as implemented by languages such as PHP and Java, do
two things:

* They _declare_ that a class meets a particular contract
* The _enforce_ some subset of that contract

The implied contract is almost always more than what the language can
enforce; for instance, the Iterator interface enforces only that valid()
returns a boolean, but the contract is really that it should do so in some
predictable circumstances.

Nonetheless, I can definitely see advantage in having more things that a
contract can declare in a way that can be automatically enforced, so I can
definitely see where you're coming from.

The problem is, PHP (following the lead of Java and others) doesn't really
implement interfaces as contracts, but as a limited form of multiple
inheritance - the implementing class inherits a bunch of abstract methods,
and is obliged to override them only if it's not itself abstract.

So in order to represent "must add this attribute", I think we would need a
syntax for "abstract attributes" on the interface, then a check that a
concrete class has no abstract attributes. That's particularly true if you
want to enforce that a *method* has an attribute, because an abstract class
can implement interfaces without actually defining their methods, so the
obligation needs to be inherited until you reach a concrete class.

That in turn opens up the question of whether attributes should be
inherited in general, to which the current RFC says "no" on grounds of
simplicity, and leaves it up to the userland implementation.

Maybe this is the kind of thing that can be experimented with in userland,
then added to core later if it can be made universal enough. For instance,
you could have "meta" attributes that took a class name as a parameter,
such as << Require(Table::class) >> on interfaces, and << Inherit >> on
attribute definitions. Then a checking tool could look up the inheritance
tree and identify missing attributes to complain about, or inherited
attributes to copy.

Regards,
-- 
Rowan Tommins
[IMSoP]

Reply via email to