Laravel Eloquent 模型覆盖静态启动方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27847163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent model overriding static boot method
提问by Chris Muench
I want to override model events and found this example code but am not sure I understand it completely.
我想覆盖模型事件并找到此示例代码,但不确定我是否完全理解它。
SOURCE:
来源:
http://driesvints.com/blog/using-laravel-4-model-events/
http://driesvints.com/blog/using-laravel-4-model-events/
There is a static method with another static method in it...How does that work? Or is it setting a static property in the boot method somehow?
有一个静态方法和另一个静态方法......它是如何工作的?或者它是否以某种方式在引导方法中设置了静态属性?
<?php
class Menu extends Eloquent {
protected $fillable = array('name', 'time_active_start', 'time_active_end', 'active');
public $timestamps = false;
public static $rules = array(
'name' => 'required',
'time_active_start' => 'required',
'time_active_end' => 'required'
);
public static function boot()
{
parent::boot();
static::saving(function($post)
{
});
}
}
回答by lukasgeiter
static::saving()
just calls the static method saving
on itself (and parent classes if not existent in current class). So it is essentially doing the same as:
static::saving()
只调用saving
自身的静态方法(如果当前类中不存在父类)。所以它本质上与以下内容相同:
Menu::saving(function($post){
});
So it is registering a callback for the saving
event within the boot function.
所以它正在为saving
引导函数中的事件注册一个回调。