In the previous tutorial, we have implemented the public configuration items. In this section we will implement user-defined extension functions. We all know that although many functions and various classes have been defined in the framework, sometimes it does not fully meet our needs, so we need to introduce self-defined classes and functions.
1. Add configuration to the framework configuration file config.php
//载入Common/Lib目录下的文件,可以载入多个
'AUTO_LOAD_FILE'=><span style="color: #0000ff">array</span>(),
2. Define a private static method in the Application class file to use the C function to read the user-defined configuration file and load it in a loop, as follows:
private static function _import_user_file(){ $fileArr=C('AUTO_LOAD_FILE'); if(is_array($fileArr) || !empty($fileArr)){ foreach ($fileArr as $v) { require_once COMMON_LIB_PATH.'/'.$v; } } }
3. Call the run method of the Application class file
//载入用户自定义的文件
self::_import_user_file();
4. Users write custom functions or classes and place them under the Common/Lib directory in the root directory, such as function1.php, People.class.php
5. Configure user-defined files in the public configuration file Common/Config/config.php
'AUTO_LOAD_FILE'=><span style="color: #0000ff">array</span>('function1.php','People.class.php'),
6. Call function methods or instantiate classes in the controller
<span style="color: #008000">//</span><span style="color: #008000">类</span> <span style="color: #800080">$peopleobj</span>=<span style="color: #0000ff">new</span><span style="color: #000000"> People(); </span><span style="color: #800080">$peopleobj</span>-><span style="color: #000000">run(); </span><span style="color: #008000">//</span><span style="color: #008000">函数</span> p(<span style="color: #800080">$_SERVER</span>);
At this point, the user-defined extension function has been implemented. Thanks to Mr. Wangma’s tutorial...