search
HomePHP FrameworkThinkPHPCome and learn about the super function app() in TP6!

The following tutorial column of thinkphp framework will introduce to you the super function app() in TP6. I hope it will be helpful to friends in need!

Come and learn about the super function app() in TP6!

##Assistant function in tp6

app() is a helper function with extremely broad coverage, deep coverage, and huge effects. It can call many methods and is also a collection of many helper functions.

First of all, this is a helper function defined in helper.php. Let’s take a look:

if (!function_exists('app')) {
    /**
     * 快速获取容器中的实例 支持依赖注入
     * @param string $name        类名或标识 默认获取当前应用实例
     * @param array  $args        参数
     * @param bool   $newInstance 是否每次创建新的实例
     * @return object|App
     */
    function app(string $name = '', array $args = [], bool $newInstance = false)
    {
        return Container::getInstance()->make($name ?: App::class, $args, $newInstance);
    }}

If you print this function directly without any parameters, it will look like this:

halt (app());

Come and learn about the super function app() in TP6!

directly calls the

App class under think without parameters.

When you bring the parameters, it calls the container class

Container.

The container class contains most of the 27 dependency injection classes we commonly use.

As follows:

Come and learn about the super function app() in TP6!We can inject new classes into the container at any time.
For example, we inject an Upgrade class into the container:

bind('settings','app\admin\controller\Upgrade');或者:bind('settings',Upgrade::class);或者:
Container::getInstance()->bind('settings', Upgrade::class)
As shown in the figure:


Come and learn about the super function app() in TP6! Call the class in the container:
Calling method:

app('settings')->upgradeTask($this->request);或:
Container::getInstance()->make('settings')->upgradeTask($this->request)
That is to say, the app() helper function can not only directly obtain the methods in the App class, but also obtain the methods in all dependency injection classes in the container. Containers are tools used to more conveniently manage class dependencies and run dependency injection.

Dependency injection essentially refers to the rapid instantiation of other classes that need to be used in the constructor.
Dependency injected classes are uniformly managed by the container.

For example, we have many assistant functions:

Assistant function description
abort	中断执行并发送HTTP状态码
app	快速获取容器中的实例 支持依赖注入
bind	快速绑定对象实例
cache	缓存管理
class_basename	获取类名(不包含命名空间)class_uses_recursive	获取一个类里所有用到的traitconfig	获取和设置配置参数
cookie	Cookie管理
download	获取\think\response\Download对象实例
dump	浏览器友好的变量输出
env	获取环境变量
event	触发事件
halt	变量调试输出并中断执行
input	获取输入数据 支持默认值和过滤
invoke	调用反射执行callable 支持依赖注入
json	JSON数据输出
jsonp	JSONP数据输出
lang	获取语言变量值
parse_name	字符串命名风格转换
redirect	重定向输出
request	获取当前Request对象
response	实例化Response对象
session	Session管理
token	生成表单令牌输出
trace	记录日志信息
trait_uses_recursive	获取一个trait里所有引用到的traiturl	Url生成
validate	实例化验证器
view	渲染模板输出
display	渲染内容输出
xml	XML数据输出
app_path	当前应用目录
base_path	应用基础目录
config_path	应用配置目录
public_path	web根目录
root_path	应用根目录
runtime_path	应用运行时目录
In front of the APP() assistant function, it can only be regarded as a younger brother, because only one app() function can be used Replaces most helper functions.

Example:

例如我们调用配置文件:
config()
实际我们可以这样写:app('config')->get()又例如:request()->time() 可以写作:app('request')->time()

For other helper functions, app() packs them all in the same way.

The above is the detailed content of Come and learn about the super function app() in TP6!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!