Hi,
Stanislav Malyshev schrieb:
What I still don't understand it's how you can solve these conflicts at
all. I.e., suppose you have Counter API and DBConnection API on each of
which you have clean() method - for Counter it resets it to zero and for
DB it resets authentication details. How it is possible to resolve it in
any way so that resulting class would support both APIs?
Would exactly do you mean with API? And how would you solve this problem
without traits? Traits can not solve stuff like colliding methods with
colliding semantics in interfaces.
One solution would be to do the following:
trait Counter {
public function clean() {
$this->setCntValue(0);
}
}
trait DBConnection {
public function clean() { // one problem we probably should not
// discuss here is the naming, think it
// should be named resetCredentials
$this->setCredentials(null);
}
}
class DBCounter {
use Counter {
!clean,
cleanCounter => clean
}
use DBConnection {
!clean,
resetCredentials => clean
}
}
This would be a class reusing the behavior implemented in both traits,
and this is the contribution of the traits idea. With mixins this
wouldn't be possible and C++ multiple-inheritance would introduce fragility.
If DBCounter should implement ICounter and IDBConnection with each
having a clean method, there is no meaningful possible implementation
within a single class. Since the semantics of the two clean methods are
not as near as it would be necessary.
and a
class DBCounter {
use Counter {
!clean,
cleanCounter => clean
}
use DBConnection {
!clean,
resetCredentials => clean
}
public function clean() {
$this->cleanCounter();
$this->resetCredentials();
}
}
would be a bit pointless, but possible...
Kind Regards
Stefan
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php