Home > Backend Development > PHP Problem > How to customize extensions in PHP (1) Basic steps

How to customize extensions in PHP (1) Basic steps

藏色散人
Release: 2023-03-14 14:30:01
forward
2708 people have browsed it

php custom extension (1)

I remember that the first time I wrote a php extension was directly from Baidu. I wrote an extension based on the Internet, but I didn’t know why. Let's look at the basic steps of an extension first, and then explore the principles behind it.

Use the source code tool to automatically generate the extended directory structure

First enter the php source code ext directory and execute the following command

/www/test/php/php/bin/php ext_skel.php --ext helloworld
cd helloworld
Copy after login

Modifyconfig.m4 Configuration file, that is, whether the extension you are writing now uses external dependencies, configure the --with-hello option, otherwise configure the --enable-hello option, and remove the comments according to your needs

dnl If your extension references something external, use 'with':
 PHP_ARG_WITH([helloworld],
   [for helloworld support],
   [AS_HELP_STRING([--with-helloworld],
     [Include helloworld support])])

dnl Otherwise use 'enable':
Copy after login

Extended function writing

Thenvim helloworld.c Write extended function code
First look at the module structure definition

zend_module_entry helloworld_module_entry = {
        STANDARD_MODULE_HEADER,
        "helloworld",                                   /* Extension name */
        helloworld_functions,                   /* zend_function_entry */
        PHP_MINIT(helloworld),                                                  /* PHP_MINIT - Module initialization */
        NULL,                                                   /* PHP_MSHUTDOWN - Module shutdown */
        PHP_RINIT(helloworld),                  /* PHP_RINIT - Request initialization */
        NULL,                                                   /* PHP_RSHUTDOWN - Request shutdown */
        PHP_MINFO(helloworld),                  /* PHP_MINFO - Module info */
        PHP_HELLOWORLD_VERSION,         /* Version */
        PHP_MODULE_GLOBALS(pib),
    NULL,
    NULL,
    NULL,
    STANDARD_MODULE_PROPERTIES_EX
};
Copy after login

Function function name collection

static const zend_function_entry helloworld_functions[] = {
        PHP_FE(helloworld_test1,                arginfo_helloworld_test1)
        PHP_FE(helloworld_test2,                arginfo_helloworld_test2)
        PHP_FE_END
};
Copy after login

Real function function code

PHP_FUNCTION(helloworld_test2)
{
        int argc = ZEND_NUM_ARGS();
        char *messages = NULL;
        size_t   messages_len = 0;
        char *context = NULL;
        size_t   context_len = 0;

        zend_string *retval;

        ZEND_PARSE_PARAMETERS_START(0, 2)
                Z_PARAM_OPTIONAL
                Z_PARAM_STRING(messages, messages_len)
                Z_PARAM_STRING(context, context_len)
        ZEND_PARSE_PARAMETERS_END();

        retval = strpprintf(0, "Hello %s test %s", messages, context);

        RETURN_STR(retval);
}
Copy after login

Function parameter definition

ZEND_BEGIN_ARG_INFO(arginfo_helloworld_test2, 0)
        ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
Copy after login

Compile and install

/www/test/php/php/bin/phpize
./configure --with-php-config=/www/test/php/php/bin/php-config
make && make install
Copy after login

Now that the file helloworld.so already exists in the PHP extension directory, add the extended configuration <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">extension = helloworld.so</pre><div class="contentsignin">Copy after login</div></div> to

php.ini

and then you can test what you have written. functionhelloworld_test2();After completing an extension, I feel that I have gained nothing. I don’t know the principle of why I wrote it like this. Let’s talk about the principle in the next part. Let’s start with The beginning of the PHP life cycle is introduced, see Next Part.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to customize extensions in PHP (1) Basic steps. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c php
source:segmentfault.com
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