# Another question on traits

- Thread: https://externals.io/message/50307
- Original archive: https://news-web.php.net/php.internals/50307
- Started by Nathan Nobbe on 2010-11-17T18:06:37+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #50307 — Nathan Nobbe — 2010-11-17T18:06:37+00:00

URL: https://externals.io/message/50307#50307

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

---

## #50308 — Stefan Marr — 2010-11-17T19:06:22+00:00

URL: https://externals.io/message/50307#50308 · In reply to: #50307

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
