Although the model of tp5 is fixed, many people will build different directory structures, some for the access method, some for the restful style, so when referencing the three-party package, the path method is also different, the following lists several commonly used. :
autoload
ThinkPHP5.0
It truly realizes on-demand loading, all class libraries adopt an automatic loading mechanism, and support class library mapping and composer
automatic loading of class libraries.
The implementation of auto-loading is done by the think\Loader
class library, and the auto-loading specification conforms to PHP's PSR-4
.
autoload
Since the new version ThinkPHP
fully adopts the feature of namespace, it is only necessary to correctly define the namespace where the class library is located, and the path of the namespace is consistent with the directory of the class library file, then the class can be automatically loaded.
The autoload detection sequence for class libraries is as follows:
1. Class library mapping detection;
2. PSR-4
Automatic loading detection;
3. PSR-0
Automatic loading detection;
The system will detect in order, and once the detection takes effect, it will automatically load the corresponding class library file.
class library mapping
If we follow our namespace definition specification above, the automatic loading of class libraries can basically be completed, but if more namespaces are defined, the efficiency will decrease. Therefore, we can define class libraries for commonly used class libraries. map. Naming the class library mapping is equivalent to defining an alias for the class file, which is more efficient than namespace positioning, for example:
Loader::addClassMap('think\Log',LIB_PATH.'think\Log.php'); Loader::addClassMap('org\util\Array',LIB_PATH.'org\util\Array.php');
You can also use addClassMap
methods to bulk import class library mapping definitions, for example:
$map = [
'think\Log' => LIB_PATH.'think\Log.php',
'org\util\array'=> LIB_PATH.'org\util\Array.php' ]; Loader::addClassMap($map);
Although classes registered through class library mapping may not be required to correspond to namespace directories, it is still recommended to define class libraries and directories in accordance with the PSR-4 specification.
class library import
If you don't need the system's auto-loading function, or you don't use namespaces, you can also manually load class library files using think\Loader
class methods, for example:import
Loader::import('org.util.array'); //import 进入 输入 导入 汇入 Loader::import('@.util.upload');
Example
// 引入 extend/qrcode.php
Loader::import('qrcode', EXTEND_PATH);
// 助手函数
import('qrcode', EXTEND_PATH); // 引入 extend/wechat-sdk/wechat.class.php Loader::import('wechat-sdk.wechat', EXTEND_PATH, '.class.php'); // 助手函数 import('wechat-sdk.wechat', EXTEND_PATH, '.class.php');
Class library imports also use a namespace-like concept (but do not require actual namespace support), supported "root namespaces" include:
content | illustrate |
---|---|
behavior | system behavior library |
think | core base class library |
traits | System Traits class library |
app | Application class library |
@ | Indicates the current module class library package |
traits traits
If you fully comply with the namespace definition of the system, in general, you don't need to manually load the class library file, just instantiate it directly.
ThinkPHP5.0
Method not recommended import
.
Composer autoload
Version 5.0 supports Composer
the automatic loading of installed class libraries, and you can directly Composer
call them directly according to the namespace in the dependent library.
autoload
ThinkPHP5.0
It truly realizes on-demand loading, all class libraries adopt an automatic loading mechanism, and support class library mapping and composer
automatic loading of class libraries.
The implementation of auto-loading is done by the think\Loader
class library, and the auto-loading specification conforms to PHP's PSR-4
.
autoload
Since the new version ThinkPHP
fully adopts the feature of namespace, it is only necessary to correctly define the namespace where the class library is located, and the path of the namespace is consistent with the directory of the class library file, then the class can be automatically loaded.
The autoload detection sequence for class libraries is as follows:
1. Class library mapping detection;
2. PSR-4
Automatic loading detection;
3. PSR-0
Automatic loading detection;
The system will detect in order, and once the detection takes effect, it will automatically load the corresponding class library file.
class library mapping
If we follow our namespace definition specification above, the automatic loading of class libraries can basically be completed, but if more namespaces are defined, the efficiency will decrease. Therefore, we can define class libraries for commonly used class libraries. map. Naming the class library mapping is equivalent to defining an alias for the class file, which is more efficient than namespace positioning, for example:
Loader::addClassMap('think\Log',LIB_PATH.'think\Log.php'); Loader::addClassMap('org\util\Array',LIB_PATH.'org\util\Array.php');
You can also use addClassMap
methods to bulk import class library mapping definitions, for example:
$map = [
'think\Log' => LIB_PATH.'think\Log.php',
'org\util\array'=> LIB_PATH.'org\util\Array.php' ]; Loader::addClassMap($map);
Although classes registered through class library mapping may not be required to correspond to namespace directories, it is still recommended to define class libraries and directories in accordance with the PSR-4 specification.
class library import
If you don't need the system's auto-loading function, or you don't use namespaces, you can also manually load class library files using think\Loader
class methods, for example:import
Loader::import('org.util.array'); //import 进入 输入 导入 汇入 Loader::import('@.util.upload');
Example
// 引入 extend/qrcode.php
Loader::import('qrcode', EXTEND_PATH);
// 助手函数
import('qrcode', EXTEND_PATH); // 引入 extend/wechat-sdk/wechat.class.php Loader::import('wechat-sdk.wechat', EXTEND_PATH, '.class.php'); // 助手函数 import('wechat-sdk.wechat', EXTEND_PATH, '.class.php');
Class library imports also use a namespace-like concept (but do not require actual namespace support), supported "root namespaces" include:
content | illustrate |
---|---|
behavior | system behavior library |
think | core base class library |
traits | System Traits class library |
app | Application class library |
@ | Indicates the current module class library package |
traits traits
If you fully comply with the namespace definition of the system, in general, you don't need to manually load the class library file, just instantiate it directly.
ThinkPHP5.0
Method not recommended import
.
Composer autoload
Version 5.0 supports Composer
the automatic loading of installed class libraries, and you can directly Composer
call them directly according to the namespace in the dependent library.