简体   繁体   中英

Laravel Static and Non-static Methods

In Laravel Framework one can use Model's Methods as Static and also Non-static, For example you can get a user from Databse like this:

User::where('id', 1)->first();

and also like this:

$user = new User();
$user->where('id', 1)->first();

how can you do this in PHP? because as far as i know a method can only be Static or Non-Static but not both.

This is being done via the magic method __callStatic which is creating a new instance of the model then calling the method on it. This particular method, where , doesn't exist on the model and is being handled by the magic method __call which is calling this method on an Eloquent Builder instance.

PHP.net Manual - OOP - Overloading __callStatic __call

Laravel - Github - Eloquent Model __callStatic

Laravel - Github - Eloquent Model __call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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