php调用嵌套函数

Tyl*_*ler 6 php function laravel

我想知道php如何实现如下功能设计:

讲座> FUNC() - > FUNC()

这是laravel验证示例.

    $validator->after(function($validator) 
    {
        if ($this->somethingElseIsInvalid()) 
        {
            **$validator->errors()->add('field', 'Something is wrong with this field!');**
        }
    });
Run Code Online (Sandbox Code Playgroud)

after()在这里做了神奇的工作吗?

以及如何创建自己的代码,可以像这样.

mas*_*Fly 5

它叫做Method Chaining.

方法链接起作用,因为类的函数或方法总是返回进一步调用另一个函数的对象.

基本上它返回它自己.

例如:

public function method1() {
    // method content ...
    return $this;
}

public function method2() {
    // method content ...
    return $this;
}
Run Code Online (Sandbox Code Playgroud)

请参阅以下链接以阅读有关方法链接的更多信息,

http://www.techflirt.com/tutorials/oop-in-php/php-method-chaining.html

你可以找到更多关于此的文章.