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

Case Analysis of ThinkPHP Alphabet function

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "case analysis of ThinkPHP letter function". In daily operation, I believe that many people have doubts about the example analysis of ThinkPHP letter function. The editor has 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 of "ThinkPHP letter function example Analysis". Next, please follow the editor to study!

Method A

Used to instantiate the controller internally, calling the format:

/ A ('[project: / /] [grouping /] module', 'controller layer name') $User = A ('User'); $User = A (' Admin://User')

After instantiating the controller, you can call the methods in the controller, but it should be noted that in the case of cross-project calls, if your operation method has special variable operations for the current controller, there will be some unknown problems, so, in general, officials recommend that the controller layer of public calls should be developed separately and do not have too many dependencies.

B method

This is a new function that comes into being with the behavior, and you can perform a behavior, such as

B ('app_begin')

It is to execute all the functions defined by this behavior before the project starts. 2 parameters are supported, and the second parameter support needs to accept an array, such as

B ('app_begin', ["name" = > "tdweb", "time" = > time ()]); C method

The C method is a method used by Think to set, obtain, and save configuration parameters, and it is used frequently.

Dynamically set configuration parameters, which are not case-sensitive, but it is recommended to maintain a uniform uppercase configuration definition specification.

C ('DB_NAME','think')

The setting of secondary configuration parameters is supported, and it is not recommended to exceed secondary configuration parameters. For example:

C ('USER.USER_ID',8)

If you want to set multiple parameters, you can use batch settings, such as:

$config ['user_id'] = 1 * config [' user_type'] = 1 * C ($config)

If the configuration parameter passed in is empty, all the parameters are obtained:

$config = C (); D method

D method should be used to instantiate custom model class, which is an encapsulation of Model class instantiation by Think 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'); F method

In fact, the F method is a subset of the S method, which is only used for simple data caching, and can only support file form, but not cache validity, because it is returned, so it is more efficient than the S method, so we also call it the fast caching method.

The characteristics of F method are:

Simple data caching

Save in file form

Load the cache by returning data

Support for subdirectory caching and automatic creation

Support for cache deletion and batch deletion

Write and read cach

F ('data','test data')

The default save starting path is DATA_PATH (this constant is located under RUNTIME_PATH.'Data/' by default), which means that the file name is DATA_PATH.'data.' The cache file of the

Note: make sure your cache identity is unique to avoid data overwriting and conflicts.

The next time you read the cached data, use:

$Data = F ('data')

We can save it as a subdirectory, for example:

F ('user/data',$data); / / cache write F (' user/data'); / / read cache

DATA_PATH.'user/data.' will be generated. Cache files, which are automatically created if the user subdirectory does not exist, or can support multi-level subdirectories, such as:

F ('level1/level2/data',$data)

If you need to specify the starting directory of the cache, you can use the following ways:

F ('data',$data,TEMP_PATH)

Delete cach

F ('data',NULL); G method

The function of G method includes two functions: marking position and interval statistics. Let's take a look at the specific usage:

Mark position

The first use of the G method is to mark the position, for example:

G ('begin')

Means to mark the current location as a begin tag, record the execution time of the current location, and record memory footprint if the environment supports it. You can call the G method tag anywhere.

Run time statistics

After marking the location, we can call the G method again for interval statistics, for example:

G ('begin'); / / Other code snippets G ('end'); / / Maybe there are other codes here / / for statistical interval echo G ('begin','end').' s'

G ('begin','end') indicates the execution time (in seconds) from the begin location to the end location. Begin must be a marked location. If the end location has not been marked at this time, the current location will be automatically marked as an end tag. The output is similar to:

0.0056s

The default statistical accuracy is 4 decimal places. If you think this statistical accuracy is not enough, you can also set it for example:

G ('begin','end',6). S'

Memory cost statistics

If your environment supports memory footprint statistics, you can also use the G method for interval memory cost statistics (in kb), for example:

Echo G ('begin','end','m').' kb'

The third parameter uses m for memory cost statistics, and the output may be:

625kbI method

As you can see, the I method is a new member of Thinkphp's many single-letter functions, named after the English Input (input). It is mainly used to obtain system input variables more easily and safely, and can be used anywhere. The usage format is as follows:

I ('variable type. Variable name', ['default'], ['filter method'])

Variable type refers to the request method or input type, including:

How get gets GET parameters post gets POST parameters param automatically determines request type gets GET, POST or PUT parameters request gets REQUEST parameters put gets PUT parameters session gets $_ SESSION parameters cookie gets $_ COOKIE parameters server gets $_ SERVER parameters globals gets $GLOBALS parameters

Note: variable types are not case sensitive. Variable names are strictly case-sensitive.

Default values and filtering methods are optional parameters.

L method

The L method is used to set and get the current language definition when multiple languages are enabled.

Call format:

L ('language variable', ['language value']) M method

The M method is used to instantiate a base model class, which differs from the D method in:

No need for custom model class, reduce IO load, better performance

Only methods in the underlying model class (default is Model class) can be called after instantiation

You can specify table prefixes, database and database connection information when instantiating

The power of the D method is reflected in how strong your encapsulated custom model class is, but as the basic model class of the new Think framework becomes more and more powerful, the M method becomes more and more practical than the D method.

The calling format of the M method:

M ('[base model name:] model name', 'data table prefix', 'database connection information') R method

The R method is used to call the operation method of a controller and is a further enhancement and supplement of the A method.

The calling format of the R method:

R ('[project: / /] [grouping /] module / operation', 'parameter', 'controller layer name')

This operation method can be called in other controllers through the R method (usually the R method is used for cross-module calls)

$data = R ('User/detail',array (' 5'))

The official suggestion is not to make too many calls in the same layer, which will cause logic confusion. The parts that are called by the public should be encapsulated into separate interfaces. With the help of the new feature of 3.1, a multi-layer controller can be added separately for interface calls.

S method

The S method also supports passing cache parameters to the current caching method, for example:

S ('data',$Data,3600,'File',array (' length'= > 10 minutes temptation = > RUNTIME_PATH.'temp/'))

In order to make it more convenient to output template files, the new version encapsulates a T function to generate template file names.

T ([resource: / /] [module @] [topic /] [controller /] operation, [view layering])

The return value of the T function is a complete template file name that can be directly used for rendering output by the display and fetch methods.

U method

The U method is used to assemble the URL address, which is characterized in that the corresponding URL address can be automatically generated according to the current URL mode and settings, and the format is:

U ('address', 'parameter', 'pseudo-static', 'whether to jump', 'show domain name')

The advantage of using the U method in a template instead of writing a dead URL address is that once your environment changes or parameter settings change, you don't need to change any code in the template.

At this point, the study of "ThinkPHP alphabet function example analysis" is over. I hope to be able to solve your 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