Re: Traits expecting interfaces implicitly leads to expensive runtime checks
| From: | Stefan Marr | Date: | Sat, 11 Dec 2010 15:25:34 +0000 |
| Subject: | Re: Traits expecting interfaces implicitly leads to expensive runtime checks | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-51006@lists.php.net to get a copy of this message | ||
Hi:
I added the proposal to the RFC.
See http://wiki.php.net/rfc/horizontalreuse#requiring_composing_class_to_implement_interface
===== Requiring Composing Class to Implement Interface =====
Traits are able to express required methods by using abstract method declarations.
An abstract method can be satisfied in varios ways, for instance by implementing it
in the composing class or by bringing it in from another Trait.
However, for traits that require complex interfaces to be satisfied, this approach is tedious
and fragile, since it requires the developer to state all used methods explicitly.
Another solution is that a Trait expresses a requirement for the composing class to implement
a certain interface. This is not entirely identical to using abstract methods, however.
First, it imposes a requirement on interface level and thus will have the same fragility
with respect to interface changes as all other clients of an interface.
On the other hand, it avoids duplications of abstract method definitions and makes
the interface the main entity of responsibility as for normal client-interface uses
in current code.
<?php
// IteratorUser works with $this using the Iterator interface
trait IteratorUser require Iterator {
function foo() {
$next = $this->next();
...
}
}
// composed into a class that needs to implement the interface
class Example implements Iterator {
use IteratorUser;
}
Are there any objections to implementing this?
Thanks
Stefan
--
Stefan Marr
Software Languages Lab
Vrije Universiteit Brussel
Pleinlaan 2 / B-1050 Brussels / Belgium
http://soft.vub.ac.be/~smarr
Phone: +32 2 629 2974
Fax: +32 2 629 3525