SoFunction
Updated on 2025-04-09

A brief discussion on the development of PHP Extension—Basics Page 2/2


Here is the specific code for info_func that needs to be written in "say_hello.c":

Copy the codeThe code is as follows:

/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(say_hello)
{
php_info_print_table_start();
php_info_print_table_header(2, "say_hello support", "enabled");
php_info_print_table_row(2, "author", "Zhang Yang"); /* Replace with your name */
php_info_print_table_end();

/* Remove comments if you have entries in
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */

You can see that we have written two lines of content, whether the component is available, and the author information.

Writing core functions
Writing the core function is divided into three steps: 1. Use the macro PHP_FUNCTION to define the function body; 2. Use the macro ZEND_BEGIN_ARG_INFO and ZEND_END_ARG_INFO to define the parameter information; 3. Use the macro PHP_FE to add the function to say_hello_functions. The following step-by-step instructions.

Define function body using macro PHP_FUNCTION
Copy the codeThe code is as follows:

PHP_FUNCTION(say_hello_func)
{
char *name;
int name_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE)
{
return;
}
php_printf("Hello %s!", name);

RETURN_TRUE;
}

As mentioned above, almost everything cannot be written naked when writing PHP extensions, but must use the corresponding macro. This can be clearly seen from the above code. Generally speaking, the core function code is generally composed of the following parts:

Define the function, this step is implemented through the macro PHP_FUNCTION. The external name of the function is the name in the brackets after the macro.

Declare and define local variables.

To parse the parameters, this step is implemented through the zend_parse_parameters function. The function is to read data from the input stack of the function user, and then convert it into corresponding function parameters and fill in variables for use by the core function code below. The first parameter of zend_parse_parameters is the number of parameters passed by the user, which can be generated by the macro "ZEND_NUM_ARGS() TSRMLS_CC"; the second parameter is a string, where each letter represents a variable type, and we only have a string type variable, so the second parameter is "s"; finally each parameter requires some necessary local variable pointers to store data. The following table gives the letters of different variable types and their required local variable pointers.

After the parameter parsing is completed, the core function code is shown. We only output one line of characters here. php_printf is the Zend version of printf.

The final return value is also implemented through a macro. The RETURN_TRUE macro returns the boolean value "true".

Use the macros ZEND_BEGIN_ARG_INFO and ZEND_END_ARG_INFO to define parameter information

Parameter information is a necessary part of the function. If you do not go into it in depth, you will directly give the corresponding code:

Copy the codeThe code is as follows:

ZEND_BEGIN_ARG_INFO(arginfo_say_hello_func, 0)
ZEND_END_ARG_INFO()

For specific information, please read the relevant macro definitions.

Use the macro PHP_FE to add the function to say_hello_functions
Finally, we need to add the function and parameter information we just defined to the say_hello_functions array, the code is as follows:
Copy the codeThe code is as follows:

const zend_function_entry say_hello_functions[] = {
PHP_FE(say_hello_func, arginfo_say_hello_func)
{NULL, NULL, NULL}
};

This step is implemented through the PHP_EF macro. Note that the last line of this array must be {NULL, NULL, NULL}, please do not delete it.

Here are all the codes for saying_hello.c after writing:
Copy the codeThe code is as follows:

/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| /license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@ so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/

/* $Id: header 297205 2010-03-30 21:09:07Z johannes $ */

#ifdef HAVE_CONFIG_H
#include ""
#endif

#include ""
#include "php_ini.h"
#include "ext/standard/"
#include "php_say_hello.h"

/* If you declare any globals in php_say_hello.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(say_hello)
*/

/* True global resources - no need for thread safety here */
static int le_say_hello;

/* {{{ PHP_FUNCTION
*/
PHP_FUNCTION(say_hello_func)
{
char *name;
int name_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE)
{
return;
}
php_printf("Hello %s!", name);

RETURN_TRUE;
}

ZEND_BEGIN_ARG_INFO(arginfo_say_hello_func, 0)
ZEND_END_ARG_INFO()
/* }}} */

/* {{{ say_hello_functions[]
*
* Every user visible function must have an entry in say_hello_functions[].
*/
const zend_function_entry say_hello_functions[] = {
PHP_FE(say_hello_func, arginfo_say_hello_func)
{NULL, NULL, NULL} /* Must be the last line in say_hello_functions[] */
};
/* }}} */

/* {{{ say_hello_module_entry
*/
zend_module_entry say_hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"say_hello",
say_hello_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(say_hello),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_SAY_HELLO
ZEND_GET_MODULE(say_hello)
#endif

/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(say_hello)
{
php_info_print_table_start();
php_info_print_table_header(2, "say_hello support", "enabled");
php_info_print_table_row(2, "author", "Zhang Yang"); /* Replace with your name */
php_info_print_table_end();

/* Remove comments if you have entries in
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */

Compile and install the extension
Enter the following command in the say_hello directory:
Copy the codeThe code is as follows:

/usr/bin/phpize
./configure
make
make install

This completes the installation of the say_hello extension (if there is no error).

At this time, if you place the php extension directory, you will find that there is an additional file saying_hello.so. As shown in the figure below:

Here is how to add it to the configuration and restart Apache (if needed). These are all contents of PHP basic configuration, so I won't explain in detail.

Extended testing

If the above is completed successfully, when running phpinfo(), you should see the following information:

This means that the extension has been installed successfully. Then we write a test PHP script:

Copy the codeThe code is as follows:

<?php
say_hello_func('Zhang Yang');
?>

Execute this script, the result is as follows:

This means that the extension is working properly.

Summarize

This article mainly uses example methods to introduce the development foundation of PHP Extension. In the use of PHP, perhaps because it needs to support new components (such as new databases), or business or performance requirements, you will almost encounter places where PHP extensions need to be developed. If I have the opportunity in the future, I will write an article to introduce some more in-depth things about extension development, such as the extension module life cycle, INI usage, and writing object-oriented extension modules, etc.
This article is based onAttribution-Non-Commercial Use 3.0The license agreement is released, reprinting or interpretation is welcome, but the name of this article must be retainedZhang Yang(including links) and may not be used for commercial purposes. If you have any questions or authorization negotiations, pleaseContact me

Previous page12Read the full text