Is there a way to extend a trait in PHP?

前端 未结 2 1270
轻奢々
轻奢々 2021-02-01 12:18

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

2条回答
  •  盖世英雄少女心
    2021-02-01 12:31

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

提交回复
热议问题