Comparison of four methods for thinkphp instantiated objects

Source: Internet
Author: User

In thinkphp, there are four different methods of instantiating objects, new method, D method, M method, and empty model method, and the D method and M method are more commonly used. This article is mostly an official manual thing, a little done under the summary.

New method

The new method is one of the best ways to understand the common instantiation class, the parameter is optional, the table name, the table prefix, the database connection information, and most of the cases do not need to pass the parameter.

New \home\model\newmodel (' new ', ' think_ ', ' db_config ');

D method

D method thingkphp (TP) system provides a shortcut to instantiate the class, through the D method we can quickly instantiate a class and use.

Instantiation model
$User = D (' User ');
Equivalent
$User = new \home\model\usermodel ();
Perform a specific data operation
$User->select ();
When the \home\model\usermodel class does not exist, the D function attempts to instantiate the \common\model\usermodel class below the public module and, if it does not exist, instantiate the \think\model base class of the system. This is a point worth noting.

M method

D method to instantiate a model class is usually to instantiate a specific model class, if you are only the basic curd operation of the data table, using the M method instantiation, because no need to load the specific model class, so performance will be higher.

Instantiating using the M method

$User = M (' User ');
is equivalent to the following usage
$User = new \think\model (' User ');
Perform other data operations $user->select ();
If your model class has its own business logic, the M method is not supported, and even if you have defined a specific model class, the M method instantiation is directly ignored.

Instantiate an empty model class

If you only use native SQL queries, you do not need to use an additional model class to instantiate an empty model class to operate

To instantiate an empty model
$Model = new Model ();
Or using the M shortcut method is equivalent
$Model = M ();
Make a native SQL query
$Model->query (' SELECT * from think_user WHERE status = 1 ');
Summarize

We are in the process of instantiation, the D method and the M method are often used, the difference being that the M method instantiation model does not require the user to define the model class for each datasheet, and if the D method does not find the defined model class, the M method is automatically called, and the M method ignores the custom specific model class directly.

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.