Why we are not allowed to extend Traits with Classes in PHP?
For example:
Trait T { }
Class C use T {}
/*
You can extend traits somewhat. Expanding on mrlee's answer, when you use a trait you can rename its methods.
Say for example you have a "parent" trait that was defined by your framework:
trait FrameworkTrait {
public function keepMe() {
// Framework's logic that you want to keep
}
public function replaceMe() {
// Framework's logic that you need to overwrite
}
}
Simply pull in the parent trait, rename its replaceMe method to something else, and define your own replaceMe method.
trait MyTrait {
use FrameworkTrait {
FrameworkTrait::replaceMe as frameworkReplaceMe;
}
public function replaceMe() {
// You can even call the "parent" method if you'd like:
$this->frameworkReplaceMe();
// Your logic here
}
}
And then in your class:
class MyClass {
use MyTrait;
}
Now objects of this class can do this:
$obj = new MyClass();
$obj->keepMe(); // calls "parent" trait
$obj->replaceMe(); // calls "child" trait
$obj->frameworkReplaceMe(); // calls "parent" trait