Home>Article>PHP Framework> Guide to organizing ThinkPHP alphabetical functions

Guide to organizing ThinkPHP alphabetical functions

WBOY
WBOY forward
2022-03-09 18:28:53 2262browse

This article brings you relevant knowledge aboutthinkphp, which mainly introduces related issues about letter functions, including A, B, C, D methods, etc. I hope it will be helpful to everyone helpful.

Guide to organizing ThinkPHP alphabetical functions

Recommended learning: "thinkphp learning"

A method

is used to instantiate the controller internally , calling format:

// A('[项目://][分组/]模块','控制器层名称')$User = A('User');$User = A('Admin://User');

After instantiating the controller, you can call the methods in the controller. However, it should be noted that in the case of cross-project calls, if your operation method is specific to the current There will be some unknown problems with the special variable operations of the controller. Therefore, generally speaking, officials recommend that the controller layer that requires public calls be developed separately without too many dependencies.

B Method

This is a new function that comes into being with the behavior. It can execute a certain behavior. For example,

B('app_begin');

is to execute this behavior definition before the project starts. all functions. Supports 2 parameters. The second parameter needs to accept an array, such as

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

C method

C method is Think’s method for setting, obtaining, and saving configuration parameters. Frequency of use higher.

Dynamicly set configuration parameters. Configuration parameters are not case-sensitive, but it is recommended to maintain the configuration definition specifications in uniform uppercase letters.

C('DB_NAME','think');

Supports the setting of secondary configuration parameters. It is not recommended that the configuration parameters exceed the second level. For example:

C('USER.USER_ID',8);

If you want to set multiple parameters, you can use batch settings, for example:

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

If the incoming configuration parameters are empty, it means to get all parameters:

$config = C();

D method

D method should be a more commonly used method. It is used to instantiate custom model classes. It is an encapsulation of the Model class instantiation by the Think framework and implements the singleton mode. , supports cross-project and group calls, the calling format is as follows:

D('[项目://][分组/]模型','模型层名称')

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

D method can automatically detect the model class. If a custom model class exists, the custom model class will be instantiated. If it does not exist, the Model base class will be instantiated. At the same time, for the already instantiated model , will not be instantiated repeatedly. The most common use of the

D method is to instantiate a custom model of the current project, for example:

// 实例化 User 模型$User = D('User');

F method

The F method is actually a sub-child of the S method The set function is only used for simple data caching, and can only support file form and does not support cache validity period. Because it uses the return method, its efficiency is higher than the S method, so we also call it the fast cache method.

The characteristics of the F method are:

  • Simple data caching;
  • Save in file form;
  • Load the cache by returning data;
  • Supports subdirectory caching and automatic creation;
  • Supports deletion of cache and batch deletion;

Write and read cache

F('data','test data');

Default save The starting path isDATA_PATH(this constant is located underRUNTIME_PATH.'Data/'in the default configuration), which means that the file name will be generatedDATA_PATH.'data.'cache file.

Note: Make sure your cache identifier is unique to avoid data overwriting and conflicts.

The next time you read cached data, use:

$Data = F('data');

We can save it in a subdirectory, for example:

F('user/data',$data); // 缓存写入F('user/data'); // 读取缓存

will generateDATA_PATH.' user/data.'Cache file, if the user subdirectory does not exist, it will be created automatically. It can also support multi-level subdirectories, for example:

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

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

F('data',$data,TEMP_PATH);

Delete cache

F('data',NULL);

G method

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

Mark the position

The first usage of the G method is to mark the position. For example:

G('begin');

means to mark the current position as the begin tag and record the execution of the current position. Time, and if the environment supports it, memory usage can also be recorded. G method markers can be called anywhere.

Running time statistics

After marking the position, we can call the G method again to perform interval statistics, for example:

G('begin'); // ...其他代码段G('end'); // ...也许这里还有其他代码 // 进行统计区间echo G('begin','end').'s';

G('begin',' end')means counting the execution time from the begin position to the end position (unit is seconds). begin must be a marked position. If the end position has not been marked at this time, the current position will be marked automatically. is the end tag, and the output result is similar to:

0.0056s

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

G('begin','end',6).'s';

Memory overhead statistics

If your environment supports memory usage statistics, you can also use the G method to perform interval memory overhead statistics (unit is kb), for example:

echo G('begin','end','m').'kb';

The third parameter uses m to represent memory overhead. Statistics, the output result may be:

625kb

I 方法

正如你所见到的一样,I 方法是 Thinkphp 众多单字母函数中的新成员,其命名来自于英文 Input(输入),主要用于更加方便和安全的获取系统输入变量,可以用于任何地方,用法格式如下:

I('变量类型.变量名',['默认值'],['过滤方法'])

变量类型是指请求方式或者输入类型,包括:

方式 说明
get 获取 GET 参数
post 获取 POST 参数
param 自动判断请求类型获取 GET、POST 或者 PUT 参数
request 获取 REQUEST 参数
put 获取 PUT 参数
session 获取 $_SESSION 参数
cookie 获取 $_COOKIE 参数
server 获取 $_SERVER 参数
globals 获取 $GLOBALS 参数

注意:变量类型不区分大小写。变量名则严格区分大小写。

默认值和过滤方法均属于可选参数。

L 方法

L 方法用于启用多语言的情况下,设置和获取当前的语言定义。

调用格式:

L('语言变量',['语言值'])

M 方法

M 方法用于实例化一个基础模型类,和 D 方法的区别在于:

  • 不需要自定义模型类,减少 IO 加载,性能较好;
  • 实例化后只能调用基础模型类(默认是 Model 类)中的方法;
  • 可以在实例化的时候指定表前缀、数据库和数据库的连接信息;

D 方法的强大则体现在你封装的自定义模型类有多强,不过随着新版 Think 框架的基础模型类的功能越来越强大,M 方法也比 D 方法越来越实用了。

M 方法的调用格式:

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

R 方法

R 方法用于调用某个控制器的操作方法,是 A 方法的进一步增强和补充。

R 方法的调用格式:

R('[项目://][分组/]模块/操作','参数','控制器层名称')

可以通过 R 方法在其他控制器里面调用这个操作方法(一般 R 方法用于跨模块调用)

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

官方的建议是不要在同一层多太多调用,会引起逻辑的混乱,被公共调用的部分应该封装成单独的接口,可以借助3.1的新特性多层控制器,单独添加一个控制器层用于接口调用

S 方法

S 方法还支持对当前的缓存方式传入缓存参数,例如:

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

T 方法

为了更方便的输出模板文件,新版封装了一个 T 函数用于生成模板文件名。

T([资源://][模块@][主题/][控制器/]操作,[视图分层])

T 函数的返回值是一个完整的模板文件名,可以直接用于 display 和 fetch 方法进行渲染输出。

U 方法

U 方法用于完成对 URL 地址的组装,特点在于可以自动根据当前的 URL 模式和设置生成对应的 URL 地址,格式为:

U('地址','参数','伪静态','是否跳转','显示域名');

在模板中使用 U 方法而不是固定写死 URL 地址的好处在于,一旦你的环境变化或者参数设置改变,你不需要更改模板中的任何代码。

推荐学习:《thinkphp框架

The above is the detailed content of Guide to organizing ThinkPHP alphabetical functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete