Home > PHP Framework > ThinkPHP > body text

What is the return value of m method in thinkphp

WBOY
Release: 2022-04-25 10:25:37
Original
2567 people have browsed it

In thinkphp, the return value of the m method is a Model object; the m method can be understood as the abbreviation of "$m=new Model("user")", and the calling format is "M('[Basic Model Name:]Model name','Data table prefix','Database connection information')", its function is to instantiate a basic model class.

What is the return value of m method in thinkphp

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

What is the return value of the m method in thinkphp

The M method can be thought of as the abbreviation of $m=new Model("user"), that is, M() returns a Model object

The M method is used to instantiate a basic model class.

The calling format of the M method:

M('[基础模型名:]模型名','数据表前缀','数据库连接信息')
Copy after login

What are the specific uses of the M method:

1. Instantiate the basic model (Model) class

When no model is defined, we can use the following method to instantiate a model class for operation:

//实例化User模型
$User = M('User');
//执行其他的数据操作
$User->select();
Copy after login

This This method is the simplest and most efficient, because it does not need to define any model classes, so it supports cross-project calls. The disadvantage is also that there is no custom model class, so the relevant business logic cannot be written and only basic CURD operations can be completed.

$User = M('User');
Copy after login

is actually equivalent to:

$User = new Model('User');
Copy after login

means operating the think_user table. The M method also has a singleton function like the D method, and it will not be instantiated repeatedly if called multiple times. The model name parameter of the M method will be automatically converted to lowercase when converted into a data table, which means that ThinkPHP's data table naming specification is in all lowercase format.

2. Instantiate other public model classes

The first way to instantiate is because there is no definition of the model class, so it is difficult to encapsulate some additional logical methods, but In most cases, you may just need to extend some common logic, then you can try the following method.

$User = M('CommonModel:User');
Copy after login

The changed usage is actually equivalent to:

$User = new CommonModel('User');
Copy after login

Because the system's model classes can be automatically loaded, we do not need to manually import the class library before instantiation. The model class CommonModel must inherit Model. We can define some common logical methods in the CommonModel class, which eliminates the need to define specific model classes for each data table. If your project already has more than 100 data tables, most of them are basic For CURD operations, only some models have some complex business logic that needs to be encapsulated, so the combination of the first method and the second method is a good choice.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the return value of m method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template