Maison  >  Article  >  développement back-end  >  Comment personnaliser les extensions en PHP (1) Étapes de base

Comment personnaliser les extensions en PHP (1) Étapes de base

藏色散人
藏色散人avant
2021-12-20 15:47:282646parcourir

Extension personnalisée php (1)

Je me souviens que la première fois que j'ai écrit une extension php, c'était directement depuis Baidu, j'ai écrit une extension basée sur Internet, mais je ne savais pas pourquoi. Commencez par les étapes de base d'une extension, puis discutez des principes qui la sous-tendent.

Utilisez l'outil de code source pour générer automatiquement la structure du répertoire d'extension

Entrez d'abord le répertoire ext du code source php et exécutez la commande suivante

/www/test/php/php/bin/php ext_skel.php --ext helloworld
cd helloworld

Modifiez le fichier de configuration config.m4, qui c'est-à-dire si l'extension écrite maintenant est utilisée en externe. Pour en dépendre, configurez l'option --with-hello, sinon configurez l'option --enable-hello et supprimez les commentaires selon vos propres besoinsconfig.m4配置文件,就是现在写的扩展是否用到外部依赖,就配置--with-hello选项,否则配置--enable-hello选项,按照自己的需求把注释去掉

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':

扩展功能书写

然后vim  helloworld.c 进行扩展功能代码书写
先看下模块结构定义

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
};

功能函数名字集合

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

真正的功能函数代码

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);
}

函数参数定义

ZEND_BEGIN_ARG_INFO(arginfo_helloworld_test2, 0)
        ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()

编译安装

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

现在PHP的扩展目录中已经有了helloworld.so这个文件,在php.ini中添加上扩展的配置

extension = helloworld.so

然后就可以测试自己写的函数了helloworld_test2() rrreeeÉcriture de fonctions étendues

puis vim helloworld .c Écrire le code de fonction étendue
Premier coup d'œil à la définition de la structure du module
rrreee

Collection de noms de fonctions de fonction
rrreee🎜Code de fonction réel 🎜rrreee🎜Définition des paramètres de fonction🎜rrreee🎜🎜Compilez et installez🎜🎜 rrreee🎜Il y a maintenant le fichier helloworld.so dans le répertoire de l'extension PHP. Ajoutez la configuration de l'extension dans php.ini<.>🎜rrreee🎜Ensuite, vous pouvez tester ce que vous avez écrit function <code>helloworld_test2(); Après avoir terminé une extension, j'ai l'impression de n'avoir rien gagné. Je ne connais pas le principe de la raison pour laquelle j'ai écrit. comme ça. Parlons du principe dans la partie suivante. Commençons par le cycle de vie de PHP, voir 🎜article suivant🎜. 🎜🎜Apprentissage recommandé : "🎜Tutoriel vidéo PHP🎜"🎜🎜

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer