Hi,
Simas' question yesterday lead me to take a look at the RFC on the wiki and
I have a quick question.
Specifically on the 'Rejected Features' -> 'Interfaces Propagation' section.
So it sounds like implementing an interface directly with a trait has been
shot down, what I wonder about is will it still work if a class implements
an interface and uses a trait which provides the functions in said
interface?
<?php
interface IHello {
public function sayHello();
}
trait SayHello {
public function sayHello() {
echo 'hello world';
}
}
class MyHelloWorld implements IHello {
use SayHello;
}
$o = new MyHelloWorld();
var_dump($o instanceof IHello); // bool (true)
?>
An explicit way to leverage traits in the inheritance tree... Is that
accurate, or will that not work either?
thx,
-nathan
Hi Nathan:
So it sounds like implementing an interface directly with a trait has been
shot down,
Yes, Traits are meant to be compile-time only, they are not used to introduce typing relationships.
what I wonder about is will it still work if a class implements
an interface and uses a trait which provides the functions in said
interface?
Yes, sure.
<?php
interface IHello {
public function sayHello();
}trait SayHello {
public function sayHello() {
echo 'hello world';
}
}
This is a typical pattern I would expect to see in code that uses traits.
You have an interface and then you provide a trait thats provide a standard implementation for that interface. I would probably call the trait similar to the interface HelloImpl or THello or so.
class MyHelloWorld implements IHello {
use SayHello;
}$o = new MyHelloWorld();
var_dump($o instanceof IHello); // bool (true)
?>
If that does not work, it is a bug I think.
Best regards
Stefan
thx,
-nathan
--
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