It's fine if a trait depends on methods in the class into which it is embedded. But these dependencies should be explicit, by declaring an abstract function
. PHP can then check that such a method actually exists.
But traits should not override methods. That is rather confusing behaviour. Instead, give a different name to the methods in the trait. In this particular example, you are relying on a method in the base class of the class into which your trait is embedded. That is a very obscure dependency. Compare the precedence examples in the docs.
Why not use abstract classes instead? Because you can have multiple traits, but only one base class. Therefore, traits are more general, and are useful for a bundle of convenience functions that you want to use in multiple classes. Your format
method is a great example of this. Written more clearly, it would be:
trait Formatter
{
abstract public function parse($text, $format);
public function format($text)
{
$format = true;
return $this->parse($text, $format);
}
}