Home>Article>PHP Framework> 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 theThinkPHP usage tutorialcolumn 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!