Differences between M and D methods in ThinkPHP differences between M and D methods in ThinkPHP both M and D methods are used to instantiate a model class, the M method is used to efficiently instantiate a basic model class, and the D method is used to instantiate a user-defined model class. If the M method is used in the following cases, consider using the M method: the specific differences between the M method and the D method in the data table ThinkPHP
Differences between M and D methods
Both the M and D methods in ThinkPHP are used to instantiate a model class. The M method is used to efficiently instantiate a basic model class, and the D method is used to instantiate a user-defined model class.
Use M method
If the following is the case, consider using the M method:
- When performing simple CURD operations on a data table without complex business logic
- Only when some tables have complicated business logic, the M method and the instantiated CommonModel class are used in combination.
The M method can even be simply used to operate the data table corresponding to the parameter table name:
$User = M('User'); Use method D
If the following is the case, consider using the D method:
- You need to use some advanced features in the ThinkPHP model, such as the auto-verification feature (implemented in the create () method) and associated model.
- The business logic is complex and involves a large number of tables
- Define the business logic in the custom Model class (under the Lib/Model Directory), and want to implement these business logic in the operation
In addition, the method D does not support cross-project calls. you need to use the following:
$ User = D ('user', 'admin'); // instantiate the User model under the Admin project $ User = D ('admin. user'); // The Project Group is enabled.Prompt
After a project group is enabled, the Model class does not necessarily correspond to the project group. Model classes shared by multiple project teams are stored in the Model directory and can be instantiated directly using D ('modelname. And D ('user. userInfo ') does not mean that the User must be a project group, or a classification directory of the files under the Model, D ('user. userInfo ') instantiate the UserInfo model class under the User directory.
Summary
Both the M and D methods can be used directly when the model class file does not exist, but obviously the M method is more efficient. However, the business logic in the model class should be used, you must use the D method.
A more vivid analogy is that the M method is like a computer with an operating system installed, and only some basic functions can be used; the D method is like installing some application software such as Office and QQ on the installed system, which is more powerful and the whole computer runs slowly.
The above is a summary of the differences between M and D methods. M and D methods should be selected based on actual conditions.