Hi Rasmus:

On 16 Nov 2011, at 01:17, Rasmus Schultz wrote:

> I knew about the traits features in Scala, and I guess I assumed this would
> be similar - not so.

Right, they are not the same. PHP's traits are much closer to the notion
of traits introduced in the Smalltalk world, while Scala's traits are rather 
like
mixins as for instance in Ruby. Aside issues like typing and whether that type
information is preserved, the main difference is that PHP's traits do not use
implicit conflict resolution.


> I believe the key to understanding traits, is understanding that traits are
> in fact an implementation detail - an artifact that does not really change
> or affect the nature of OOP in PHP as such, and by design, should not. I
> understand that now - thank you :-)
> 
> From my perspective, a key difference from classes and interfaces, is that
> traits have no meaningful use at run-time - no type-hinting and with no
> real reflection-features that reveal the details. They cannot implement
> interfaces for classes, but classes can use them to implement interfaces.
> Traits just provide a set of method implementations that can be aggregated
> by classes. So in a sense, they're a design-time tool for the programmer.
> 

I like that explanation, thanks!


> So I guess my remaining quibble is that the documentation needs to relay
> this somehow - with a basic real-world example that actually uses an
> interface too, to clarify the difference, and to demonstrate how this tool
> use useful in conjunction with class and interface declarations. Hello
> world really doesn't explain anything, other than the syntax.

[...]

> This may be a little too magical for an example in the documentation though
> - and doesn't demonstrate interfaces.
> 
> I'll ponder this and post a better example if I can think of one... :-)

As someone else already pointed out, there are tools to contribute such nuggets 
to the documentation. Please submit a patch.


> And just one other comment:
> 
>>> var_dump($test instanceof CartBehavior); // => false
> 
>> Why would it be beneficial to throw an exception here?
> 
> CartBehavior is a trait, and not a class or interface. The instanceof
> operator has no meaning for traits - it always returns false.
> 
> You could argue that false is the expected result - I would argue that the
> only reason this happens to work at all, is because traits internally are
> classes.
> 
> Suppose you thought you were actually doing a meaningful type-check of some
> sort? If by mistake you put a trait-name where you meant to put the name of
> an interface or class, such an error could be very hard to spot.

While I agree that it can be hard to spot, I would usually advocate for less
intrusive alternatives.

Hmmm, well, I could put in a warning in strict mode, I guess.
But that would require a consistent handling within all the other APIs we got, 
too.
Especially is_a is such a candidate. And changing is_a seems to be a non-trivial
thing. So, I will need to look carefully at that.

Would be great if you could file a bug report for that.

> I hope this feedback is useful :-)
Yes it is. I think it is especially important that other people actually write 
down their understanding of the mechanism. Blog posts, mailing list posts, 
improved documentation,
they are all important.

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


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to