search
HomeBackend DevelopmentPHP TutorialThinkPHP单字母函数(快捷方法)使用总结_PHP

在ThinkPHP中有许多使用简便的单字母函数(即快捷方法),可以很方便开发者快速的调用,但是字母函数却不方便记忆,本文将所有的字母函数总结一下,以方便以后查找。

1.U() URL组装 支持不同URL模式

U($url='',$vars='',$suffix=true,$domain=false)

  @param string $url URL表达式,格式:'[模块/控制器/操作#锚点@域名]?参数1=值1&参数2=值2...'
  @param string|array $vars 传入的参数,支持数组和字符串
  @param string $suffix 伪静态后缀,默认为true表示获取配置值
  @param boolean $domain 是否显示域名
  @return string

2.D() D函数用于实例化模型类 格式 [资源://][模块/]模型

D($name='',$layer='')

  @param string $name 资源地址
  @param string $layer 模型层名称
  @return Model

3.M() M函数用于实例化一个没有模型文件的Model

M($name='',$tablePrefix='',$connection='')

  @param string $name Model名称 支持指定基础模型 例如MongoModel:User
  @param string $tablePrefix 表前缀
  @param mixed $connection 数据库连接信息
  @return Model

4.I() 获取输入参数 支持过滤和默认值

I($name,$default='',$filter=null)

  使用方法:

 I('id',0); //获取id参数 自动判断get或者post
 I('post.name','','htmlspecialchars'); //获取$_POST['name']
 I('get.'); //获取$_GET

5.B() 执行某个行为

B($name,$tag='',&$params=NULL)

  @param string $name 行为名称
  @param string $tag 标签名称(行为类无需传入)
  @param Mixed $params 传入的参数
  @return void

6.C() 读取及设置配置参数

C($name=null,$value=null,$default=null)

  @param string|array $name 配置变量
  @param mixed $value 配置值
  @param mixed $default 默认值
  @return mixed

7.E() 抛出异常处理

E($msg, $code=0)

  @param string $msg 异常消息
  @param integer $code 异常代码 默认为0
  @return void

8.G() 记录和统计时间(微秒)和内存使用情况

G($start,$end='',$dec=4)

  使用方法:

 G('begin'); // 记录开始标记位
 // ... 区间运行代码
 G('end'); // 记录结束标签位
 echo G('begin','end',6); //统计区间运行时间 精确到小数后6位
 echo G('begin','end','m'); // 统计区间内存使用情况

  如果end标记位没有定义,则会自动以当前作为标记位
  其中统计内存使用需要 MEMORY_LIMIT_ON 常量为true才有效
  @param string $start 开始标签
  @param string $end 结束标签
  @param integer|string $dec 小数位或者m
  @return mixed

9.L()获取和设置语言定义(不区分大小写)

L($name=null,$value=null)

  @param string|array $name 语言变量
  @param mixed $value 语言值或者变量
  @return mixed

10.T()获取模版文件 格式 资源://模块@主题/控制器/操作

T($template='',$layer='')

  @param string $name 模版资源地址
  @param string $layer 视图层(目录)名称
  @return string

11.N() 设置和获取统计数据

N($key,$step=0,$save=false)

  使用方法:

 N('db',1); // 记录数据库操作次数
 N('read',1); // 记录读取次数
 echo N('db'); // 获取当前页面数据库的所有操作次数
 echo N('read'); // 获取当前页面读取次数

  @param string $key 标识位置
  @param integer $step 步进值
  @return mixed

12.A()A函数用于实例化控制器

格式:[资源://][模块/]控制器

A($name,$layer='',$level='')

  @param string $name 资源地址
  @param string $layer 控制层名称
  @param integer $level 控制器层次
  @return Controller|false

13.R() 远程调用控制器的操作方法

URL 参数格式 [资源://][模块/]控制器/操作

R($url,$vars=array(),$layer='')

  @param string $url 调用地址
  @param string|array $vars 调用参数 支持字符串和数组
  @param string $layer 要调用的控制层名称
  @return mixed

14.W()渲染输出Widget

W($name,$data=array())

  @param string $name Widget名称
  @param array $data 传入的参数
  @return void

15.S()缓存管理

S($name,$value='',$options=null)

  @param mixed $name 缓存名称,如果为数组表示进行缓存设置
  @param mixed $value 缓存值
  @param mixed $options 缓存参数
  @return mixed

16.F() 快速文件数据读取和保存 针对简单类型数据 字符串、数组

F($name, $value='',$path=DATA_PATH)

  @param string $name 缓存名称
  @param mixed $value 缓存值
  @param string $path 缓存路径
  @return mixed

关于这些快捷方法的详细操作,读者可以参考本站上的相关实例教程。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor