Re: Another question on traits
| From: | Stefan Marr | Date: | Wed, 17 Nov 2010 19:06:22 +0000 |
| Subject: | Re: Another question on traits | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-50308@lists.php.net to get a copy of this message | ||
Hi Nathan:
On 17 Nov 2010, at 19:06, Nathan Nobbe wrote:
> 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