Home >PHP Framework >ThinkPHP >The difference between D method and M method in Thinkphp

The difference between D method and M method in Thinkphp

步履不停
步履不停Original
2019-07-01 11:43:143236browse

The difference between D method and M method in Thinkphp

The two have something in common: they instantiate the model, but what are the differences? Let’s take a look:

$User = D('User'); The parameter User in brackets corresponds to \Home\Model\UserModel.class.php of the corresponding model class file (we assume that the current module is Home ), if the parameter is 'UserType', then the corresponding model class file is \Home\Model\UserTypeModel.class.php, that is to say, the parameter of the D method is the name of the model, and is consistent with the case definition of the model class. .

$User = M('User'); is equivalent to $User = new \Think\Model('User'); that is, when the M method is instantiated, by default It is the \Think\Model class that directly instantiates the system. If we want to instantiate other public model classes, we can use the following method: $User = M('\Home\CommenModel:User','think_','db_config' );(We assume the table prefix is ​​think_).

In the process of instantiation, we often use the D method and the M method. The difference between the two methods is that the M method instantiates the model without the user defining a model class for each data table. If the D method is not found, The defined model class will automatically call the M method.

In addition, if you want to use the automatic verification and automatic completion functions in ThinkPHP, you need to use the D method.

In fact, to put it bluntly, the parameter when instantiating the M method is the table name of your database, while the D method instantiates the Model class you wrote in the Model folder. Of course, you want to automatically verify and complete it automatically. , using the D method.

For more ThinkPHP related technical articles, please visit the ThinkPHP usage tutorial column to learn!

The above is the detailed content of The difference between D method and M method in Thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more