
Eloquent/Model
ActiveRecord
魔术方法 __get和__set
1
2
3
4
5
6
7
8
9
10
|
public function __get($key)
{
return $this->getAttribute($key);
}
public function __set($key, $value)
{
$this->setAttribute($key, $value);
}
|
魔术方法 __call和__callStatic,查询器
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public function __call($method, $parameters)
{
if (in_array($method, ['increment', 'decrement'])) {
return $this->$method(...$parameters);
}
return $this->newQuery()->$method(...$parameters);
}
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}
|
Trait Evaquent
复写了魔术方法__call
1
2
3
4
5
6
7
8
9
10
11
|
public function __call($method, $parameters)
{
// 每次调用查询都会先boot
$this->bootEavquentIfNotBooted();
if ($this->isAttributeRelation($method)) {
return call_user_func_array($this->attributeRelations[$method], $parameters);
}
return parent::__call($method, $parameters);
}
|
近期评论