ThinkPHP3.1 D Method example _ php instance

Source: Internet
Author: User
This article mainly introduces the use of the d method of ThinkPHP3.1. If you need a friend, you can refer to the D method, which should be a lot of methods used to instantiate a custom model class, it is an encapsulation of the Model class instantiated by the ThinkPHP framework and implements the singleton mode. It supports cross-project and group calls. The call format is as follows:

D ('[Project: //] [group/] model', 'model layer name ')

The method returns an instantiated model object.

The D method can automatically detect Model classes. If a custom Model class exists, the user-defined Model class is instantiated. If the class does not exist, the Model base class is instantiated, at the same time, models that have already been instantiated will not be instantiated repeatedly.

D. The most common method is to instantiate a custom model of the current project. For example:

// Instantiate the User model $ User = D ('user ');

The Lib/Model/UserModel. class. php file under the current project will be imported, and then the UserModel class will be instantiated. Therefore, the actual code may be equivalent to the following:

import('@.Model.UserModel');$User = new UserModel();

However, if the D method is used, if the UserModel class does not exist, it will automatically call

new Model('User');

In addition, you do not need to instantiate the object again during the second call, which can reduce the overhead of Object Instantiation.

The D Method supports cross-group and project instantiation models, for example:

// Instantiate the User model D ('admin: // user') of the Admin project // instantiate the User model D ('admin/user') of the Admin group ')

Note: To Call The model across projects, make sure that the directory structure of the two projects is parallel.

ThinkPHP version 3.1 and later versions. Due to the added support for layered models, the D method can also instantiate other models.For example:

// Instantiate UserService class $ User = D ('user', 'service'); // instantiate UserLogic class $ User = D ('user', 'lock ');

D ('user', 'service'); imports Lib/Service/UserService. class. php and instantiates it, which is equivalent to the following code:

import('@.Service.UserService');$User = new UserSerivce();

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.