What is the difference between instantiating objects m () and D () in thinkphp? thinkphp How do I instantiate an object?

Source: Internet
Author: User

In the process of instantiation, the D method and M method are often used, the difference between the two methods is that the M method instantiation model does not require the user to define a model class for each data table, and the M method is automatically called if the D method does not find a defined model class.
Popular Point says:
The M instantiation parameter is the table name of the database.
D instantiates the model file you created underneath the Model folder.

For example: $user = new Usermodel ();equivalent to $user = D (' user ');If an empty model is instantiatedfor example $Demo = new Model ();then it is equivalent to $Demo = M ();


------------------------------------------------------------------------
The difference between D and M is mainly:
The M method does not need to create a model class file, and the M method does not read the model class, so automatic validation is not valid by default, but can be achieved by dynamically assigning values
The D method must have a model class created.

We can use the following two ways to create a mapping object for a data table
First type: $Test = D (' Test ')
Second type: $Test = new Model (' Test ')
Although both of these can be select,insert,delete,udpate operations on the data,
Data validation is very different, in the first instance a model will have a data check function, if the title is not filled 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 corresponding model in the validation conditions);

If we use the second kind, there's no.

There are 1 differences when using the $truetablename, you must use the $test=d (' test '), indicating that the query is the test table,

If you are using $test=m (' test '), it means that the data side of the query is think_test.

thinkphp2.0 version test has such a problem as above
-----------------------------------------------------------------------------------------------
Popular Point says:
D is to instantiate a model based on the model file.
M is dynamically instantiating a model object by directly instantiating the model method (thinkphp base class), even if the corresponding model file does not exist
============================================================================
A quick instantiation of the Action class library
B Execution Behavior Class
C Configuration Parameter access method
D Fast instantiation of the Model class library
F Fast and simple text data access method
L Language Parameter access method
M fast High performance instantiation model
R quickly remotely invoke the Action class method
s Fast Cache access method
U URL dynamic Generation and redirection method
W Fast Widget Output method

The D function instantiates the module below the Lib/model of your current project.
If the module does not exist, return the object that instantiates the model directly (meaning is the same as the M () function).
and M only returns, instantiating the model object. Its $name parameter handles the operation of the database as a table name for the database.

What is the difference between instantiating objects m () and D () in thinkphp? thinkphp How do I instantiate an object?

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.