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
<?php function testfunction(){ echo 'testfunction'; } public function test(){ testfunction(); }
Display results:
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:
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
<?php function getInfo(){ phpinfo(); }
public function test(){ getInfo(); }
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文件名’);
<?php function sayhello($who){ echo 'hello ' . $who; }
public function test(){ //load load('@/hello'); //调用函数 sayhello(world); }
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!