Deep understanding of M methods in thinkphp

Source: Internet
Author: User
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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.