简体   繁体   English

在 PHP 中链接方法并将第一个调用的方法作为最后一个执行,它是如何工作的?

[英]Chaining methods in PHP and execute the first called method as last, how does it work?

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.我现在使用 Laravel 有一段时间了,从一开始我就想知道他们如何能够以随机顺序链接方法并且仍然将整个链作为一个操作执行。

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. command方法首先被调用,但是该命令只会每三十分钟运行一次。 That information came after calling the command method, but is still processed before executing it.该信息在调用command方法之后出现,但在执行它之前仍在处理。 The same goes for the before method. before方法也是如此。 That method is called last, but the some-other-command command is still being executed first.该方法最后被调用,但some-other-command命令仍然首先被执行。

I've searched the internet for the answer, but I couldn't find one.我在互联网上搜索了答案,但找不到。 I hope you know the answer.我希望你知道答案。

That method is called last, but the some-other-command command is still being executed first.该方法最后被调用,但 some-other-command 命令仍然首先被执行。

Because that is what the before() method does, placing another command before (hence the name) the current command.因为这就是before()方法所做的,在当前命令之前(因此得名)放置另一个命令。 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.正如类名Scheduler所暗示的那样,它正在设置一些时间表,而不是按原样执行代码,所以问题是对代码的作用的误解。

It depends on the context where you are using the chaining, in your example the first method is command :这取决于您使用链接的上下文,在您的示例中,第一种方法是命令

Add a new Artisan command event to the schedule.将新的 Artisan 命令事件添加到计划中。

And it returns an Event , this Event instance has a lot of methods that you can call in chaining mode because they return $this witch mean they return the current instance of the event, so that you can call an other method that the Event` class offers.它返回一个Event ,这个 Event 实例有很多你可以在链接模式下调用的方法,因为它们返回$this witch 意味着它们返回事件的当前实例,这样你就可以调用 Event` 类的另一个方法提供。

In your example在你的例子中

Schedule the event to run every thirty minutes.安排事件每三十分钟运行一次。
Return Value: $this返回值:$this

Register a callback to be called before the operation .注册一个在操作之前调用的回调。
Return Value: $this返回值:$this

Concerning the order you have to call command first to get the Event instance, and for the tow other methods the order has no effect.关于您必须首先调用command以获取Event实例的顺序,对于其他两个方法,该顺序无效。

Like if you told to someone go to the market every thirty minutes and before each time close the house door, it's the same thing if you told him (or her) before you go to the market close the house door and go every thirty minutes.就像你告诉某人每 30 分钟去一次市场并在每次关门之前告诉他(或她)在你去市场之前关上房门并每 30 分钟去一次也是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM