Home>Article>PHP Framework> Several file loading methods in ThinkPHP

Several file loading methods in ThinkPHP

尚
forward
2020-04-13 09:07:24 3129browse

Several file loading methods in ThinkPHP

ThinkPHP system provides three file loading methods:

Function library form loading:

Function library and then ThinkPHP It includes three categories: system function library file (functions.php), application-level function library file, and group-level function file.

Of these three types of function library files, only the system function library file (functions.php) exists by default in the system, while the other two types of function library forms need to be created by yourself.

At the same time, please note that only the system function library file is named functions.php, while the other two types of function library format files are named function.php.

Case: Use function library form to define the functions to be used. Function name: testfunction


      

Display results:

Several file loading methods in ThinkPHP

Functions in the defined function library file follow the requirements of PHP’s built-in function syntax when used , just write the function name (parameters) directly.

Instructions:

First: There is no need to introduce function.php, the system automatically introduces the file function.php file for us during execution;

Second: If If a function is defined in an application-level function library file, it can be used in all groups (the entire application); if a function is defined in a function library file of a certain group, it can only be used in the current group, otherwise the function will be reported Not defined.

Dynamic loading through configuration items:

In the execution process of the system, a file will be executed to App.class.php

static public function init() { // 加载动态应用公共文件和配置 load_ext_file(COMMON_PATH);

In A load_ext_file function is executed in this method.

This method is not a PHP built-in method, but is defined in the system function library file:

Several file loading methods in ThinkPHP

function load_ext_file($path) { // 加载自定义外部文件 if($files = C('LOAD_EXT_FILE')) { $files = explode(',',$files); foreach ($files as $file){ $file = $path.'Common/'.$file.'.php'; if(is_file($file)) include $file; } }

Extension: C method

C method is also one of the quick methods. Its function is to operate the configuration item in ThinkPHP:

C(name,value); Set the value of the configuration item name, the value is value

C (name); Read the value of the configuration item name

C(); Read all configuration items

Configuration items:

are defined in the application-level configuration file The configuration item LOAD_EXT_FILE introduces the file info.php.

'load_ext_file' => 'info', //包含文件名的字符串,多个文件名通过使用英文半角逗号分割

Define an info.php in the application function library file directory


      

Several file loading methods in ThinkPHP

##Test:

public function test(){ getInfo(); }

Display results:

Several file loading methods in ThinkPHP

It should be noted that the same files have been introduced in the system encapsulation method, so when using specific functions, there is no need to introduce the files separately. You need to directly write the function name to be used in the form of a function library file and pass the corresponding parameters.

Loading through the load method

Syntax:

load(‘@/不带后缀的php文件名’);

It should be noted that the file must exist in the group-level function library directory, and only Can be used in defined groups.

Case: Create the file hello.php in the group directory yourself, then define a function in it, and then use the load method to load and use the function.


      

Use the load method in the controller to load the hello.php file:

public function test(){ //load load('@/hello'); //调用函数 sayhello(world); }

Display effect:

Several file loading methods in ThinkPHP

Instructions: the above three All file loading methods can be used during actual development, but generally the first method is the main one (automatic loading in the form of a function library).

Recommended tutorial:

thinkphp tutorial

The above is the detailed content of Several file loading methods in ThinkPHP. 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