This article mainly briefly introduces several methods of thinkphp's instantiation model, as well as the key points of their use. It is very good. Recommended to everyone.
thinkphp instantiation model provides us with three methods
The first type is more commonly used
That’s $user=new Model(‘user’); //It’s also equal to $user=M(‘user’)
The second is if we have a public model
This is how we use it
For example, let’s create a new model CommonModel.class.php, a public model
This is how we instantiate it
The code is as follows:
In this way we can both instantiate the user model and call methods in the public model
The third one is for our instantiated model
Create a model UserModel.class.php
The instantiation is like this $user=new UserModel();
That’s it. Mainly when we have a lot of business logic, we use it like this
The fourth type Why is there the fourth type of empty model
The code is as follows:
Then let’s use sql statements
The code is as follows:
D method can automatically detect the model class when using it. If it does not exist, an exception will be thrown and it will not be instantiated repeatedly. It can only support the instantiation of the current project or application
That’s about all the instantiation, let’s continue to tell you about thinkphp
Summary:
The model definition and instantiation introduced in this article are more theoretical, but in subsequent development, the instantiation of the model will be used for data operations, and different model instantiation methods can be adopted according to the actual situation of the project.