功能链在PHP中

时间:2016-03-06 22:54:14

标签: php laravel

我正在尝试创建一个将在我的应用程序的各个部分中使用的函数。但我需要链接一些方法,如laravel链接其全局响应函数的方式,如下面的

response()->json(['data'=>'stuck with chaining functions']);

我需要实现类似这样的东西

db_logger()->create("user","Raise work order");
db_logger()->deleted("user","Updated wored order with id 1");
但是我被卡住了。这是我到目前为止所尝试的但是要知道成功

use App\Models\LogSheet;

if(!function_exists('db_logger')){

 function db_logger(){

    function created($user, $activity){
        saveLog($user, $activity, "created");
    };

    $updated = function ($user, $activity){
        saveLog($user, $activity, "updated");
    };

    $deleted = function ($user, $activity){
        saveLog($user, $activity, "deleted");
    };

    $read = function ($user, $activity){
        saveLog($user, $activity, "");
    };

    $log = function ($user, $activity){
        saveLog($user, $activity, "");
    };

    $saveLog = function ($user, $activity,$activity_type){
        return LogSheet::create(compact('user','activity','activity_type'));
    };

  }
}  

但无济于事。

NB:我知道类方法链接。我想要功能链的laravel风格。

1 个答案:

答案 0 :(得分:0)

对于功能更改,您必须返回$ this,Class() - > create() - > finish() 类必须设置为单例,而create必须返回$ this