Is there a way to extend trait in PHP?

大憨熊 提交于 2019-12-03 06:28:33

问题


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

Precisely I want to extend Laravel SoftDeletes trait to make SaveWithHistoryfunction, so it will create a copy of a current state of a record as deleted record. I also want to extend it with record_made_by_user_idfield.


回答1:


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


来源:https://stackoverflow.com/questions/40299080/is-there-a-way-to-extend-trait-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!