Hi,

Christian Schneider schrieb:
I can see two
use cases for aliasing:
> 1) An interface uses a different name than the
trait implements. Not sure that's a software design problem trait should
try to solve.
Well, no, aliasing shouldn't be used to achieve this, think it would be better to add some glue code to a class to fulfill a interface, i.e. add a new method which will call the method from the trait if the name of the traits method does not fit to the interface. This will avoid confusion about the next point you've named. (for simple situations exclude and aliasing will do the job as well, yes)

> 2) There is a clash with an existing function in the
class. Then one would have to both alias and exclude the trait function
name anyway (which is the same as renaming) PLUS you'd have the problem
of trait classes calling the 'wrong' functions again.
In my opinion it is not a problem, but an opportunity.

For a situation where two traits are implementing stuff which is very closely related and will be combined in a class you can do the following:

trait A {
  public function handle($something) {
    $this->process($something);
  }
  protected function process($something) {
    // ...
  }
}

trait B {
  protected function process($something) {
    // a method to process $something and be reused
    // interdependently at several places
  }
}

class Processor {
  use A {
    processA => process
  }
  use B {
    processB => process
  }
  protected function process($something) {
    // do combined processing using both traits methods
    $this->processA($something);
    $this->processB($something);
  }
}

Kind Regards
Stefan

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

Reply via email to