php擴充初探

來源:互聯網
上載者:User

標籤:php 擴充

1.首先產生擴充架構結構

Cd /alidata/tmp/php-5.6.23/ext

./ext_skel --extname=myext --proto=myext.fun

1)此時會產生相關檔案

Configure config.h include myext.c config.m4

2)主要修改檔案

Config.m4

 

PHP_ARG_WITH(myext, for myext support,

Make sure that the comment is aligned:

[  --with-myext             Include myext support])

 

dnl Otherwise use enable:

 

PHP_ARG_ENABLE(myext, whether to enable myext support,

Make sure that the comment is aligned:

[  --enable-myext           Enable myext support])

 

myext.c

 

1)頭部檔案

#ifdef HAVE_CONFIG_H

#include "config.h"

#endif

 

#include "php.h"

#include "php_ini.h"

#include "ext/standard/info.h"

#include "php_myext.h"

#include<stdio.h>

#include<malloc.h>    //記憶體管理調用

 

2)變數定義

static int le_myext;

 

3)定義函數

const zend_function_entry myext_functions[] = {

        PHP_FE(myext,   NULL)           /* For testing, remove later. */

        PHP_FE(myext_fork,NULL)

        PHP_FE(myext_list,NULL)

        //PHP_PE(myext_insert,LinkList* list, LinkListNode* node, int pos)

        //PHP_PE(myext_get,LinkList* list, int pos)

        //PHP_PE(myext_create,LinkList*)

        //PHP_PE(myext_destroy,LinkList* list)

 

        PHP_FE_END      /* Must be the last line in myext_functions[] */

};

 

4)zend模組定義

zend_module_entry myext_module_entry = {

        STANDARD_MODULE_HEADER,

        "myext",

        myext_functions,     //代表載入定義的函數@標識

        PHP_MINIT(myext),

        PHP_MSHUTDOWN(myext),

        PHP_RINIT(myext),               /* Replace with NULL if there‘s nothing to do at request start */

        PHP_RSHUTDOWN(myext),   /* Replace with NULL if there‘s nothing to do at request end */

        PHP_MINFO(myext),

        PHP_MYEXT_VERSION,

        STANDARD_MODULE_PROPERTIES

};

 

 

5)實現匯出函數

 

PHP_FUNCTION(myext_list)

{       

        long lptr;   

        double dptr;

        

        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &lptr, &dptr) == FAILURE) {

                return;

        }

        zend_printf("lptr value ad %ld",lptr);

        zend_printf("dptr value ad %ld",dptr);

}

 

6)接受使用者傳遞的參數

6.1)取得參數個數

ZEND_NUM_ARGS() TSRMLS_CC  //zend_api.h中有定義

6.2)取得實體參數

 

 

 

 

//定義變數

long lptr;

double dptr;

        

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &lptr, &dptr) == FAILURE) {

       return;

}

 

&lptr,:表示接受的參數

&dptr:表示接受的參數

 

//參數列印

zend_printf("lptr value ad %ld",lptr);

zend_printf("dptr value ad %ld",dptr);

 

 

7)完整的匯出函數實現

 

PHP_FUNCTION(myext_list)

{       

        long lptr;

        double dptr;

        

        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &lptr, &dptr) == FAILURE) {

                return;

        }

        zend_printf("lptr value ad %ld",lptr);

        zend_printf("dptr value ad %ld",dptr);

}

 

 

 

2.編輯擴充

/alidata/server/php-5.6.23/bin/phpize   壓縮擴充

./configure --with-php-config=/alidata/server/php-5.6.23/bin/php-config  編譯安裝擴充

 

/alidata/server/php-5.6.23/bin/php -m   顯示已經安裝的擴充

 

3.修改php.ini

[xhprof]

extension=xhprof.so

xhprof.output_dir=/tmp

 

 

extension=swoole.so

 

extension=myext.so

 

extension=php_list.so

 

 

4.php調用

<?php

myext_list()

?>

 

 


本文出自 “Linux營運” 部落格,請務必保留此出處http://2853725.blog.51cto.com/2843725/1948992

php擴充初探

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.