How the abstract class Model in laravel handles calls to a static dynamic method, such as ":: find ()", ":: where ()", etc.

I study how some functions are implemented in laravel because I want to understand some methods and principles of software development.

I understand that when calling a static method, such as "App \ User :: find (1) or App \ User :: whereId (1)", it is done on an eloquent model that the abstract model class implements the magic __callStatic method like this:

/**
 * Handle dynamic static method calls into the method.
 *
 * @param  string  $method
 * @param  array  $parameters
 * @return mixed
 */
public static function __callStatic($method, $parameters)
{
    $instance = new static;
    return call_user_func_array([$instance, $method], $parameters);
}

I also understand that this line is '$ instance = new static;' makes an instance of some eloquent model in which a static call was made, for example, App \ User.

, : "call_user_func_array ([$ instance, $method], $parameters);".

, , call_user_func_array() $(, find ($ parameters)) eloquent (, App\user).

, , , . , "blah()":

App\User::blah();

"BadMethodCallException " undefined Illuminate\Database\Query\Builder:: fisd() '".

"Builder" ?

"BadMethodCallException ' undefined Illuminate\Database\Eloquent\Model:: fisd()'" "?

, Builder.

+4
1

, __call , , newQuery(), Builder.

__call, __callStatic, ( ) .

, find() , __callStatic, , find() , , __call , , Builder.

, !

+4

Source: https://habr.com/ru/post/1668886/


All Articles