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!
##Assistant function in tp6app() 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.
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); }}
halt (app());
class under think without parameters.
.
As follows:
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)
Call the class in the container:
Calling method:
app('settings')->upgradeTask($this->request);或: Container::getInstance()->make('settings')->upgradeTask($this->request)
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.
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 应用运行时目录
Example:
例如我们调用配置文件: config() 实际我们可以这样写:app('config')->get()又例如:request()->time() 可以写作:app('request')->time()
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!