thinkphp shortcut Methods D and M differences
Simply put, using the M method, there is no need to define the corresponding model class (even if it is not read), usually such a model can only invoke some of the system base model class in some of the methods. The use of the D method to instantiate the model, you must have a corresponding model class file, you can call some model custom methods or properties, in addition to the Mode corresponding to the file has automatic validation or functions such as the business logic must also use D.
another analogy. M is a newly installed operating system, only the system comes with the application There are no apps installed on your own (so you can call only the built -in Model properties and methods provided) D is a number of third-party applications that have been installed Richer in use (you can invoke the properties and methods defined by the model class itself) but obviously, after installing a lot of third-party applications, the performance of the system is reduced, the execution slows down, but the functionality is obviously powerful.
?
D Method used to quickly create an instance of a model object, and singleton , for example:
$User = D ("User");
equivalent to
$User = new Usermodel ();
?
m The instantiation parameter is the table name of the database .
D instantiates a model file that you set up under the Models folder yourself .
?
D and M differ mainly in:
M method does not need to create a model class file, theM method does not read the model class, so the automatic validation is not valid by default, but can be implemented by dynamic assignment;
While the D method must have a model class created, we can use the following two methods to create a data table mapping object
First type:$Test = D (' Test ')
Second type:$Test = new Model (' Test ')
Although both of these can be select,insert,delete,udpate operation of the data, there is a big difference in data validation, in the first instance a model will have the data check function, if the title Do not fill in the words will prompt " Please enter the title " (this is a TP provides an automatic verification function, of course, also need to define the appropriate model in the validation conditions);
D the method can automatically detect the model class and will throw an exception when it does not exist. At the same time, the instantiated model is not repeated (singleton).
the default D method can only support the invocation of a model under the current project (or an application)
?
For example: $user = new Usermodel ();
equivalent to $user = D (' user ');
?
If an empty model is instantiated
For example $Demo = new Model ();
then it is equivalent to $Demo = M ();