Home  >  Article  >  PHP Framework  >  Add your own class library in thinkphp5

Add your own class library in thinkphp5

尚
forward
2020-04-14 09:03:234448browse

The class library is the core part of ThinkPHP, and ThinkPHP manages all system class libraries together through the concept of base class library. The core base class library includes basic classes and common tool classes necessary to complete the universal development of the framework.

Add your own class library in thinkphp5

thinkPHP running process

Enter from the tp5/public/index.php entry file and load the framework boot file /tp5/thinkphp/start.php

Initialize constants, register various required mechanisms, and load configuration files. After the preparation is completed, it can be executed through the run() method of the

/tp5/thinkphp/library/think/App.php class.

Preparation

Modify the framework and avoid modifying the core code. The general method is to modify and call in a specific directory.

ThinkPHP provides a specific directory tp5/extend; that needs to introduce other modifications (of course, you can also redefine EXTEND_PATH in the entry file) to customize the modification directory. It is recommended that you do not modify it if you can.

Example

Specific requirements: Introduce a custom pagination class Pagination.php to thinkphp. Then call the paging class to write business code.

Method 1: Use namespace to automatically load

Class file placement directory: tp5/extend/page/admin/Pagination.php

Bind class files according to the directory Namespace (psr-4 rules):

<?php
 namespace page\admin;
     class pagination 
     { 
           ………………………………
      }

In layman's terms, the above means automatically loading the extended class library, which requires the use of a namespace, and the namespace must correspond to the directory.

The root directory is the directory name starting from the extend directory.

Call

$page = new page\admin\pagination();

or

use page\admin\pagination;
$page = new pagination();

Method 2: Not using namespace

If the class file does not have a namespace, it cannot be loaded automatically. You must use the Loader class for manual loading

use \think\Loader;
Loader::import(&#39;page.admin.pagination&#39;);
$page = new pagination();

Recommended tutorial: thinkphp tutorial

The above is the detailed content of Add your own class library in thinkphp5. 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