What is the usage of m in php tp framework
The m method in the php tp framework is used to instantiate a basic model class. Its usage is: 1. Instantiate the basic model (Model) class; 2. Instantiate other public model classes; 3. Pass in Table prefix, database and other information.
The operating environment of this article: Windows7 system, thinkphp v5.1 version, DELL G3 computer
What is the usage of m in the php tp framework?
Detailed explanation of ThinkPHP functions: M method
The M method is used to instantiate a basic model class. The difference from the D method is:
1. There is no need to customize the model class, which reduces IO loading and has better performance;
2. After instantiation, only methods in the basic model class (the default is the Model class) can be called;
3. You can specify the table prefix, database and database connection information when instantiating; the power of the
D method is reflected in how strong the custom model class you encapsulate is, but with the basic model of the new version of ThinkPHP framework The functions of classes are becoming more and more powerful, and the M method is more and more practical than the D method.
The calling format of the M method:
M('[基础模型名:]模型名','数据表前缀','数据库连接信息')
Let’s take a look at 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();
This method is the simplest and most efficient, because there is no need to define any model class, so it is supported 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');
is actually equivalent to:
$User = new Model('User');
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, maybe If you just need to extend some general logic, you can try the following method.
$User = M('CommonModel:User');
The changed usage is actually equivalent to:
$User = new CommonModel('User');
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.
3. Pass in table prefix, database and other information
The M method has three parameters. The first parameter is the model name (can include basic model classes and databases), and the second parameter is Used to set the prefix of the data table (leave it blank to take the table prefix of the current project configuration), and the third parameter is used to set the currently used database connection information (leave it blank to take the database connection information of the current project configuration), for example:
$User = M('db2.User','think_');
means instantiating the Model model class and operating the think_user table in the db2 database.
If the second parameter is left blank or not passed, it means using the data table prefix in the current project configuration. If the data table being operated does not have a table prefix, you can use:
$User = M('db1.User',null);
represents an instance Transform the Model model class and operate the user table in the db1 database.
If the database you operate requires different user accounts, you can pass in the connection information of the database, for example:
$User = M('User','think_','mysql://user_a:1234@localhost:3306/thinkphp');
represents the basic model class using Model, and then to operate the think_user table, use user_a The account is used to connect to the database, and the operating database is thinkphp.
The third connection information parameter can use DSN configuration or array configuration, and can even support configuration parameters.
For example, if
'DB_CONFIG'=>'mysql://user_a:1234@localhost:3306/thinkphp';
is configured in the project configuration file, you can use:
$User = M('User','think_','DB_CONFIG');
The basic model class and database can be used together, for example:
$User = M('CommonModel:db2.User','think_');
If we want to instantiate a hierarchical model, using the public model class, we can use:
M('UserLogic:User');
to instantiate UserLogic, although this does not make much sense, because we can use
D('User','Logic');
Achieve the same function.
Recommended study: "thinkphp tutorial"
The above is the detailed content of What is the usage of m in php tp framework. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
