在PHP中链接方法并执行第一个被调用的方法,它是如何工作的?(Chaining methods in PHP and execute the first called method as last,

编程入门 行业动态 更新时间:2024-10-27 05:28:51
在PHP中链接方法并执行第一个被调用的方法,它是如何工作的?(Chaining methods in PHP and execute the first called method as last, how does it work?)

我现在和Laravel合作了一段时间,从一开始我一直想知道他们如何能够以随机顺序链接方法并仍然将整个链作为一个操作执行。

例如,在控制台内核中:

protected function schedule(Schedule $schedule)
{
    $schedule->command('some-command')
             ->everyThirtyMinutes()
             ->before(function (Schedule $schedule) {
                 $schedule->command('some-other-command');
             });
}
 

首先调用command方法,但该命令每30分钟才会运行一次。 调用command方法后会出现该信息,但在执行之前仍会处理。 before一种方法也是如此。 该方法最后调用,但some-other-command命令仍在执行。

我在互联网上搜索了答案,但我找不到答案。 我希望你知道答案。

I'm working with Laravel for a little while now and since the start I have been wondering how they are able to chain methods in a random order and still execute the whole chain as one operation.

For example, in the console kernel:

protected function schedule(Schedule $schedule)
{
    $schedule->command('some-command')
             ->everyThirtyMinutes()
             ->before(function (Schedule $schedule) {
                 $schedule->command('some-other-command');
             });
}
 

The command method is called first, but the command will only run every thirty minutes. That information came after calling the command method, but is still processed before executing it. The same goes for the before method. That method is called last, but the some-other-command command is still being executed first.

I've searched the internet for the answer, but I couldn't find one. I hope you know the answer.

最满意答案

该方法最后调用,但some-other-command命令仍在执行。

因为这是before()方法所做的,所以在当前命令之前放置另一个命令(因此命名)。 正如类名称Scheduler所暗示的那样,它正在设置一些调度,而不是按原样执行代码,所以问题在于对代码的作用有误解。

That method is called last, but the some-other-command command is still being executed first.

Because that is what the before() method does, placing another command before (hence the name) the current command. And as the class name Scheduler implies, it's setting up some schedule, not executing the code as is, so the question is rather a misunderstanding of what the code does.

更多推荐

本文发布于:2023-08-04 16:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417776.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   第一个   它是   链接   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!