Is there a way to extend a trait in PHP?

Yevgeniy Afanasyev

I want to use functionality of an existing trait and create my own trait on top of it only to later apply it on classes.

I want to extend Laravel SoftDeletes trait to make SaveWithHistory function, so it will create a copy of a record as a deleted record. I also want to extend it with record_made_by_user_id field.

Filip Koblański

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
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extend a Dynamic Trait?

From Dev

Extend object inside of trait in Scala

From Dev

Scala extend trait field not found

From Dev

php trait using another trait

From Dev

php trait using another trait

From Dev

Scala OO- optionally extend a trait

From Dev

Extend, Implement, or Trait to prevent reference loop

From Dev

Is it possible to extend a default method implementation of a trait in a struct?

From Dev

Instantiating all classes that extend trait in Scala

From Dev

Extend, Implement, or Trait to prevent reference loop

From Dev

Scala OO- optionally extend a trait

From Dev

Is there a way to implement a trait on top of another trait?

From Dev

Is there a way to extend type declarations?

From Dev

How to extend a dictionnary in a such way?

From Dev

Extend conditions in a dynamic way

From Dev

Use a trait conditionally in PHP

From Dev

UML representation of PHP trait

From Dev

PHP Laravel: Trait not found

From Dev

PHP Trait colliding constructor

From Dev

Namespace, Trait, and Use in PHP

From Dev

Use a trait conditionally in PHP

From Dev

PHP trait method conflicts: trait "inheritance" and trait hierarchies

From Dev

Is there a way to omit the lifetimes for the trait here?

From Dev

Idiomatic Way To Merge Trait Functionality

From Dev

Is there a way to get an implicit manifest to a trait

From Dev

Is there a way to get an implicit manifest to a trait

From Dev

Why if I extend the App trait in Scala, I override the main method?

From Dev

In scala is it possible for a trait to extend a class which needs parameters?

From Dev

Extend PHP Array with __toString()