Gregory Beaver wrote:
So, as you can see, all of the trait methods are available as regular
methods except for A::a which is overridden by B::a. If the order were
reversed, i.e. "traits B, A" then "$a->a()" would instead call A::a.
I could see this becoming a problem when it plays along with namespaces.
Let me modify your example a little bit to show what I mean.
Some/Name/Space/A.php
<?php
namespace Some::Name::Space;
trait A {
function myfunc(){ echo 'myfunc';}
function a(){ echo 'Some::Name::Space::A::a';}
}
?>
Some/Other/Name/Space/A.php
<?php
namespace Some::Other::Name::Space;
trait A {
function a(){ echo 'Some::Other::Name::Space::B::a';}
function another(){echo 'another';}
}
?>
Blah.php
<?php
class Blah extends... implements...
traits
Some::Name::Space::A,
Some::Other::Name::Space::A {
}
$a = new Blah();
$a->myfunc(); // myfunc
$a->a(); // Some::Other::Name::Space::B::a
$a->another(); // another
$a->{'Some::Name::Space::A::a'}() // Some::Name::Space::A::a
?>
I think this could lead to some problems that namespacing is trying to
solve. Also, what happens if you decide to refactor one of the traits
and change its name or namespace. You would then have to search for
every use of an "autoaliased" method. I think the original proposal is
much better in this regard, because you only have to find the classes
that use the trait.
Another thing that bothers me about this idea is that the order that
traits are added to a class determines which methods the class will
have. This seems more and more like a mixin. I like the idea that the
programmer has to make the choice over which implementation. It should
be easy for the programmer to decide on one of these three:
1. Use one of the trait's implementations of the method and exclude
all others (and possibly provide an alias to them).
2. Override the method in the class with a custom implementation.
3. Alias each of the methods and use them in the overridden method
implementation.
One more thing that I do not like about the auto aliasing is flattening.
If I want to flatten all the traits into my class, I don't know of a way
to make a method called {'Some::Name::Space::A::a'} inside the class in
userspace (even though the interpreter can obviously handle it.
--
"Joshua Thompson" <[EMAIL PROTECTED]>
<http://www.schmalls.com>
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php