Home > php教程 > php手册 > body text

【23】 Implementation of user-defined extension functions

WBOY
Release: 2016-11-30 23:59:37
Original
1339 people have browsed it

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目录下的文件,可以载入多个
Copy after login
'AUTO_LOAD_FILE'=><span style="color: #0000ff">array</span>(),
Copy after login

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

 

3. Call the run method of the Application class file

//载入用户自定义的文件
Copy after login
self::_import_user_file();
Copy after login

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

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

At this point, the user-defined extension function has been implemented. Thanks to Mr. Wangma’s tutorial...

source:php.cn
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!