Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the thinkphp big d method

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use the thinkphp big d method". In the daily operation, I believe that many people have doubts about how to use the thinkphp big d method. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the thinkphp big d method". Next, please follow the editor to study!

Big d in thinkphp is a built-in method for instantiating custom model classes, which is an encapsulation of Model class instantiation by ThinkPHP framework. The calling format of D method is "D ('[project: / /] [grouping /] model', 'model layer name')".

This article operating environment: Windows7 system, ThinkPHP5.0 version, Dell G3 computer.

Detailed explanation of ThinkPHP function: d method

D method:

D method should be used to instantiate custom model class, which is an encapsulation of Model class instantiation by ThinkPHP framework, and implements singleton pattern, which supports cross-project and group calls. The call format is as follows:

D ('[project: / /] [grouping /] model', 'model layer name')

The return value of the method is the instanced model object.

The D method can automatically detect the model class. If there is a custom model class, the custom model class is instantiated. If it does not exist, the Model base class is instantiated, and the instantiated model is not repeatedly de-instantiated.

The most common use of the D method is to instantiate a custom model for the current project, such as:

/ / instantiate the User model $User = D ('User')

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

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

But if you use the D method, if the UserModel class does not exist, it will be called automatically

New Model ('User')

And the second call does not need to be instantiated again, which can reduce a certain amount of object instantiation overhead.

The D method can support model instantiation across groups and projects, such as:

/ / instantiate User model D ('Admin://User') of Admin project / / instantiate User model D (' Admin/User') of Admin grouping

Note: to implement the cross-project invocation model, you must ensure that the directory structure of the two projects is juxtaposed.

Since version 3.1, due to the addition of support for hierarchical models, the D method can also instantiate other models, such as:

/ / instantiate UserService class $User = D ('User','Service'); / / instantiate UserLogic class $User = D (' User','Logic'); D ('User','Service')

Lib/Service/UserService.class.php is imported and instantiated, which is equivalent to the following code:

Import ('@ .Service.UserService'); $User = new UserSerivce (); at this point, the study on "how to use the thinkphp big d method" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report