In the thinkphp tutorial, I can explain the m method. I personally think that the tutorial here is only for us how to apply it, but it does not give us a deep understanding of the method he wrote.
In the thinkphp tutorial, the m method can be explained. I personally think that the tutorial here is only for us how to apply it, and we have not thoroughly understood the method he wrote:
When no model is defined, we can use the following method to instantiate a model class for operations:
// Instantiate the User model
$ User = M ('user ');
// Perform other data operations
$ User-> select ();
This method is the simplest and most efficient. because no model class needs to be defined, cross-project calls are supported. The disadvantage is that there is no custom model class, so you cannot write the relevant business logic and can only perform basic CURD operations.
Thinkphp manual provides a simple explanation of this, but profound analysis is not available. now let's take a deeper look.
Function M ($ name = '', $ tablePrefix ='', $ connection = ''){
Static $ _ model = array (); // defines an array of static variables,
If (strpos ($ name, ':') {// checks whether ':' exists to determine whether to specify the basic model.
List ($ class, $ name) = explode (':', $ name); // The list function assigns values to the split strings.
} Else {
$ Class = 'model ';
}
If (! Isset ($ _ model [$ name. '_'. $ class])
$ _ Model [$ name. '_'. $ class] = new $ class ($ name, $ tablePrefix, $ connection); // check whether the model already exists. If no model exists, save the newly defined class model to a static variable.
Return $ _ model [$ name. '_'. $ class]; // return the newly defined model
}