Is there a way to extend a trait in PHP?
Yes, there is. You just have to define new trait like this:
trait MySoftDeletes
{
use SoftDeletes {
SoftDeletes::saveWithHistory as parentSaveWithHistory;
}
public function saveWithHistory() {
$this->parentSaveWithHistory();
//your implementation
}
}
I have different approach. ParentSaveWithHistory
is still applicable method in this trait so at least should be defined as private.
trait MySoftDeletes
{
use SoftDeletes {
saveWithHistory as private parentSaveWithHistory;
}
public function saveWithHistory()
{
$this->parentSaveWithHistory();
}
}
Consider also 'overriding' methods in traits:
use SoftDeletes, MySoftDeletes {
MySoftDeletes::saveWithHistory insteadof SoftDeletes;
}
This code uses method saveWithHistory
from MySoftDeletes
, even if it exists in SoftDeletes
.