Thinkphp instantiation model _ PHP Tutorial

Source: Internet
Author: User
Let's talk about the instantiation model of thinkphp. This article briefly introduces several methods for instantiating thinkphp models and their usage points. it is very good and recommended to you. Thinkphp instantiation model

This article briefly introduces several methods for instantiating the thinkphp model and their usage points. it is very good and is recommended to you.

The thinkphp instantiation model provides three methods.

The first method is more common.

$ User = new Model ('user'); // $ user = M ('user ')

Second, if we have a public model

This is what we use.

For example, we create a model CommonModel. class. php public model.

This is what we instantiate.

The code is as follows:


$ User = new CommonModel ('user ');

In this way, the user model can be instantiated and the methods in the public model can be called.

The third is our instantiation model.

Create a model UserModel. class. php

The instantiation is like this $ user = new UserModel ();

In this way, we can use it when there are many business logic.

Fourth, why is there the fourth null model?

The code is as follows:


$ User = new Model (); // equivalent to $ user = M ();

Then let's use SQL statements.

The code is as follows:


$ List = $ user-> query ('select * from wd_user ');

D. The model class can be automatically detected when the method is used. if the model class does not exist, an exception will be thrown and the model class will not be instantiated repeatedly. it can only be instantiated for the current project or application.

There are so many instantiation cases. let's continue to talk about thinkphp.

Summary:

The model definition and instantiation introduced in this article are biased towards theory. However, in subsequent development, data operations require model instantiation, different model instantiation methods can be adopted based on the actual situation of the project.

This article introduces several methods for instantiating thinkphp models and their usage points. it is very good and recommended to you ....

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.