How to initialize extensions in PHP?

coldplay.xixi
Release: 2023-03-01 19:52:01
Original
2724 people have browsed it

PHP initialization extension method: first modify the prototype declaration near the user space function in [php_hello.h]; then modify the [hello.c] file; then add a [# at the top of the file [hello.c] include]; finally modify [hello_world] to use the value of INI.

How to initialize extensions in PHP?

How to initialize the extension in PHP:

Initialize the configuration of the extension through php.ini


Zend engine provides two ways to manage ini values.

Suppose you want to define a value in php.ini for your extension, hello.greeting, which holds the value that will be used in the hell_world() function Greeting string. You need to add some code to hello.c and php_hello.h, and make some key changes to the hell_module_entry structure.

Related learning recommendations: PHP programming from entry to proficiency

1. The first step: modify php_hello.h Close to the prototype declaration of the user space function

 PHP_MINIT_FUNCTION(hello);
PHP_MSHUTDOWN_FUNCTION(hello);// 初始化关键点
PHP_FUNCTION(hello_world);// 其它通用
PHP_FUNCTION(hello_long);
Copy after login

2. Step 2: Modify the hello.c file

to remove the ## in the current version #hello_module_entry Replace it with the following list:

zend_module_entry hello_module_entry = {
     #if ZEND_MODULE_API_NO >= 20010901
          STRANDARD_MODULE_HEADER,
     #endif
          PHP_HELLO_WORLD_EXTENAME,
          hello_functions,
          PHP_MINIT(hello),
          PHP_MSHUTDOWN(hello),
          NULL,NULL,NULL,
     #if ZEND_MODULE_API_NO >= 20010901
          PHP_HELLO_WORLD_VERSION,
     #endif
          STANDARD_MODULE_PROERTIES
}
 
PHP_INI_BEGIN()
PHP_INI_ENTRY("hello.greeting","hello world",PHP_INI_ALL,NULL)
PHP_INI_END()
 
PHP_MINIT_FUNCTION(hello)
{
     REGISTER_INI_ENTRIES();
     return SUCCESS;
}
 
PHP_MSHUTDOWN_FUNCTION(hello)
{
     UNREGISTER_INI_ENTRIES();
     return SUCCESS;
}
Copy after login

3. Now, you only need the

#includes at the top of the file hello.c Add a #include next to , so that the correct header file that supports INI can be activated:

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_hello.h"
Copy after login

4. Finally, you You can modify

hello_world to use the value of INI:

PHP_FUNCTION(hello_world)
{
     RETURN_STRING(INI_STR("hello.greeting"),1);
}
Copy after login

However: you will have to copy from

INI_STR()The value returned. This is because, before entering the PHP variable stack, it is a static string. In fact, if you try to modify the returned string, the PHP execution environment will become unstable or even crash.

Summary:

  • is actually using

    INI_STR() to obtain the hello.greeting entry The current string.

  • If you want to get other parameters, you can use functions like

    INI_INT, etc. Due to different setting parameters, it may cause functions like ini_set hard to use.

The above is the detailed content of How to initialize extensions in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template