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

Several file loading methods in ThinkPHP

Release: 2020-04-13 09:07:24
forward
3259 people have browsed it

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

<?php
 
function testfunction(){
	echo &#39;testfunction&#39;;
}
    public function test(){
        testfunction();
    }
Copy after login

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

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(&#39;LOAD_EXT_FILE&#39;)) {
        $files      =  explode(&#39;,&#39;,$files);
        foreach ($files as $file){
            $file   = $path.&#39;Common/&#39;.$file.&#39;.php&#39;;
            if(is_file($file)) include $file;
        }
    }
Copy after login

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.

    &#39;load_ext_file&#39;         =>  &#39;info&#39;,     
    //包含文件名的字符串,多个文件名通过使用英文半角逗号分割
Copy after login

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

<?php
 
function getInfo(){
	phpinfo();
}
Copy after login

Several file loading methods in ThinkPHP

##Test:

    public function test(){
        getInfo();
    }
Copy after login

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文件名’);
Copy after login

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.

<?php
 
function sayhello($who){
	echo &#39;hello &#39; . $who;
}
Copy after login

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

    public function test(){
        //load
        load(&#39;@/hello&#39;);
        //调用函数
        sayhello(world);
    }
Copy after login
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!

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