Lukas Kahwe Smith wrote:
>
>> I have been uncomfortable with the current trait suggestions because
>> they occur in the body of the class, whereas extends/implements is
>> outside.  I think there is a way to handle this, however.
>
> I dont agree here. traits are different. They do not affect "what" the
> class is. They change what it can do and as such the stuff should be
> defined next to properties and methods.
>>
>
>> <?php
>>
>> trait trait1 { function a(){}}
>> trait trait2 { function a(){}}
>> class Blah extends ... implements ... traits trait1, trait2, trait3 {
>> }
>> ?>
>
> Here it gets worse. Now if I refactor things into a trait, I have to
> start changing all uses of those methods.
>
> @Greg: I think you are going in the wrong direction here. 

I think you may be confused, because your statement about refactoring is
inaccurate - for most methods, there would be no change from the current
proposal.  In other words, in my example above, you could use
trait1::a() simply as Blah::a, for instance:

<?php
$a = new Blah;
$a->a();
?>

Only if one wanted to access trait2::a() would one need to either alias
via "function trait2::a as b" to use as "$a->b()" or explicitly refer to
"$a->{'trait2::a'}()"

To be absolutely clear, here is how it could work:

<?php
trait A {
function myfunc(){ echo 'myfunc';}
function a(){ echo 'A::a';}
}
trait B {
function a(){ echo 'B::a';}
function another(){echo 'another';}
}
class Blah extends... implements... traits A, B {
}
$a = new Blah();
$a->myfunc(); // myfunc
$a->a(); // B::a
$a->another(); // another
$a->{'A::a'}() // A::a
?>

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.

The current proposal would cause the above to fail with a name conflict
error rather than automatically aliasing.

Here is another example with the overriding syntax:

<?php
trait A {
function myfunc(){ echo 'myfunc';}
function a(){ echo 'A::a';}
}
trait B {
function a(){ echo 'B::a';}
function another(){echo 'another';}
}
class Blah extends... implements... traits A, B {
function B::a as private;
function A::a as a;
}
$a = new Blah();
$a->myfunc(); // myfunc
$a->a(); // A::a
$a->another(); // another
?>

Greg

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

Reply via email to