The model class can use static calls or instantiate calls in two ways, such as:
//Static Call$user= User::get (1);$user->name = ' thinkphp ';$user-save ();//instantiating a model$user=NewUser;$user->name= ' thinkphp ';$user-save ();//using the Loader class instantiation (singleton)$user= Loader::model (' User ');//or use the helper function ' model '$user= Model (' User ');$user->name= ' thinkphp ';$user->save ();
Second, model initialization
1, the model also supports initialization, and controller initialization is different, the initialization of the model is rewritten Model initialize , specifically as follows
namespace App\index\model; Use Think\model; class extends model{ // Custom Initialize protectedfunction Initialize () { // need to call ' Model ' of ' Initialize ' method parent::Initialize ( ); // TODO: Custom Initialization }}
2, you can also use static init methods, it is important to note that init only when the first instantiation of the execution, and the method needs to pay attention to the static call specification, specifically as follows
namespace App\index\model; Use Think\model; class extends model{ // Custom Initialize protectedstaticfunction init () { //TODO: Custom initialization }}
ThinkPHP5.0 model Calls