# typehinting traits

- Thread: https://externals.io/message/55848
- Original archive: https://news-web.php.net/php.internals/55848
- Started by Ferenc Kovacs on 2011-10-17T17:41:45+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #55848 — Ferenc Kovacs — 2011-10-17T17:41:45+00:00

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

Hi Stefan,

Multiple people asked me that how they can expect/check that a given object
uses a trait or not.
Of course one could write the concrete methods as a trait and always use a
given interface for typehints, but I can't see why shouldn't instanceof and
typehints in general work for traits.
Is the any technical or maybe theoretical argument against this?

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #55849 — Stefan Marr — 2011-10-17T18:00:22+00:00

URL: https://externals.io/message/55848#55849 · In reply to: #55848

Hi Ferenc:



> Hi Stefan,
> 
> Multiple people asked me that how they can expect/check that a given object uses a trait or not.
> Of course one could write the concrete methods as a trait and always use a given interface for typehints, but I can't see why shouldn't instanceof and typehints in general work for traits.
> Is the any technical or maybe theoretical argument against this? 

Originally I proposed traits including the semantics that traits directly implement interfaces.

The discussion is somewhere here: http://www.mail-archive.com/internals@lists.php.net/msg33935.html

I think Sebastian was the person with the strongest opinion on that particular issue:
   http://www.mail-archive.com/internals@lists.php.net/msg33948.html

And I tend to agree with him.

Currently I think along the following lines:

  A trait is not a full unit of reuse!
  A class is. 
  Don't use traits where you should use classes and composition.
  Traits do not guarantee anything, if you want to be sure your invariants hold, use classes and composition.

  Traits allow to reuse behavior in a much more flexible way, but they do not replace classes.

And, well, then there is this: https://wiki.php.net/rfc/horizontalreuse#requiring_composing_class_to_implement_interface

That should make sure that people can use interfaces for the purpose envisioned here.

For reference, there is also: https://bugs.php.net/bug.php?id=55613


But aside of the things I said before:
 - theoretically: keeping interfaces and traits apart is a good, clean language design decision.
 - practically: people LOVE ugly languages, because they get things done

So, well, I can't claim anything about value here.
The current situation might be more friendly to the teacher.
Another design might offer more freedom/power... 


Best regards
Stefan


> 
> -- 
> Ferenc Kovács
> @Tyr43l - http://tyrael.hu

-- 
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

---

## #55850 — Ferenc Kovacs — 2011-10-17T18:19:09+00:00

URL: https://externals.io/message/55848#55850 · In reply to: #55849

> Hi Ferenc:
>
> 
>
> > […]
> object uses a trait or not.
> > […]
> a given interface for typehints, but I can't see why shouldn't instanceof
> and typehints in general work for traits.
> > […]
>
> Originally I proposed traits including the semantics that traits directly
> implement interfaces.
>
> The discussion is somewhere here:
> http://www.mail-archive.com/internals@lists.php.net/msg33935.html
>
> I think Sebastian was the person with the strongest opinion on that
> particular issue:
>   http://www.mail-archive.com/internals@lists.php.net/msg33948.html
>
> And I tend to agree with him.
>
> Currently I think along the following lines:
>
>  A trait is not a full unit of reuse!
>  A class is.
>  Don't use traits where you should use classes and composition.
>  Traits do not guarantee anything, if you want to be sure your invariants
> hold, use classes and composition.
>
>  Traits allow to reuse behavior in a much more flexible way, but they do
> not replace classes.
>
> And, well, then there is this:
> https://wiki.php.net/rfc/horizontalreuse#requiring_composing_class_to_implement_interface
>
> That should make sure that people can use interfaces for the purpose
> envisioned here.
>
> For reference, there is also: https://bugs.php.net/bug.php?id=55613
>
>
> But aside of the things I said before:
>  - theoretically: keeping interfaces and traits apart is a good, clean
> language design decision.
>  - practically: people LOVE ugly languages, because they get things done
>
> So, well, I can't claim anything about value here.
> The current situation might be more friendly to the teacher.
> Another design might offer more freedom/power...


Thanks for the clarification.
Without either having the ability to check a trait or implement an interface
for a trait, or at least implementing the require syntax, I think that it
isn't really a non-fragile solution for my question.
AFAIK the only solution available currently is something like:

<?php
interface testTraitInterface{

}

trait testTrait{

}

class testClass implements testTraitInterface{
use testTrait;
}

$tc = new testClass;
var_dump($tc instanceof testTraitInterface);


Which is pretty weak of a solution, however if we only thinks traits as a
macro, this is enough, but then why bother with the as and insteadof, traits
composed traits and such?
I think we will have much more feedback with the release of 5.4, so maybe we
can re-consider extending the implementation for the next version.
And I agree that it is the best to only release what we are confident in,
and I'm happy that we waited a version for correctly implementing Closures
for objects.

Thanks again for the clarification and for your work on traits!

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #55851 — Ferenc Kovacs — 2011-10-17T20:59:49+00:00

URL: https://externals.io/message/55848#55851 · In reply to: #55850

>
>
> 
>
> > […]
>
>
> Thanks for the clarification.
> Without either having the ability to check a trait or implement an
> interface for a trait, or at least implementing the require syntax, I think
> that it isn't really a non-fragile solution for my question.
> AFAIK the only solution available currently is something like:
>
> <?php
> interface testTraitInterface{
>
> }
>
> trait testTrait{
>
> }
>
> class testClass implements testTraitInterface{
> use testTrait;
> }
>
> $tc = new testClass;
> var_dump($tc instanceof testTraitInterface);
>
>
> Which is pretty weak of a solution, however if we only thinks traits as a
> macro, this is enough, but then why bother with the as and insteadof, traits
> composed traits and such?
> I think we will have much more feedback with the release of 5.4, so maybe
> we can re-consider extending the implementation for the next version.
> And I agree that it is the best to only release what we are confident in,
> and I'm happy that we waited a version for correctly implementing Closures
> for objects.
>
> Thanks again for the clarification and for your work on traits!
>

Jeff Carouth pointed out(
https://twitter.com/#!/jcarouth/status/126030715514138624) that we have
 `class_uses()` (https://bugs.php.net/bug.php?id=55266), would be nice having
that in the documentation.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #55852 — Stefan Marr — 2011-10-17T21:50:13+00:00

URL: https://externals.io/message/55848#55852 · In reply to: #55851

Hi:



> > […]
> 
> Jeff Carouth pointed out(
> https://twitter.com/#!/jcarouth/status/126030715514138624) that we have
> `class_uses()` (https://bugs.php.net/bug.php?id=55266), would be nice having
> that in the documentation.

Until now I did not even know I had karma...

Here you go:
http://news.php.net/php.doc.cvs/8942 


And well, I have neither a clue about the toolchain nor anything related to that XML format.

So, a review not only content but also style-wise is more than welcome.

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
