Home > PHP Framework > ThinkPHP > Come and learn about the super function app() in TP6!

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

藏色散人
Release: 2020-07-14 13:14:20
forward
7894 people have browsed it

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);
    }}
Copy after login

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)
Copy after login

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)
Copy after login

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	应用运行时目录
Copy after login
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()
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template