首頁  >  文章  >  後端開發  >  PHP擴充開發-LINUX環境的程式碼實例分享

PHP擴充開發-LINUX環境的程式碼實例分享

黄舟
黄舟原創
2017-03-22 10:05:001379瀏覽


LINUX環境下開發PHP擴充的步驟如下:

1、下載PHP源碼,解壓縮,我的解壓縮目錄是:/root /lamp/php-5.5.37

2、cd到/root/lamp/php-5.5.37/ext目錄下,建立檔案test_extension.def檔案

int a(int x, int y)string b(string str, int n)

3、透過擴展框架生成器產生框架目錄:
ext_skel –extname=test_extension –proto=test_extension.def
產生成功結果如下:

Creating directory test_extension
awk: /root/lamp/php-5.5.37/ext/skeleton/create_stubs:56: warning: escape sequence `\|' treated as plain `|'
Creating basic files: config.m4 config.w32 .svnignore test_extension.c php_test_extension.h CREDITS EXPERIMENTAL tests/001.phpt test_extension.
php [done].To use your new extension, you will have to execute the following steps:
1.  $ cd ..
2.  $ vi ext/test_extension/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-test_extension
5.  $ make
6.  $ ./sapi/cli/php -f ext/test_extension/test_extension.php
7.  $ vi ext/test_extension/test_extension.c
8.  $ make
Repeat steps 3-6 until you are satisfied with ext/test_extension/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

4、切換到產生的框架目錄下:cd test_extension
5、修改設定檔config.m4,去掉10、11、12行前面的dnl,如下

PHP_ARG_WITH(test_extension, for test_extension support,
Make sure that the comment is aligned:
[  --with-test_extension             Include test_extension support])

6、實作函數a和b的功能,vi test_extension.c,修改後函數a、b如下

PHP_FUNCTION(a)
{        
int argc = ZEND_NUM_ARGS();        
long x;        
long y;        
if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)
        {
                php_error(E_WARNING, "zend_parse_parameters failure!");                
                return;
        }
        RETURN_LONG(x + y);
}

PHP_FUNCTION(b)
{        
char *str = NULL;        
int argc = ZEND_NUM_ARGS();        
int str_len;        
long n;        
char *result;        
char *ptr;        
int result_length;        
if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
        {
        
                php_error(E_WARNING, "zend_parse_parameters failure!");                
                return;
        }
        result_length = str_len * n;
        result = (char *) emalloc(result_length + 1);
        ptr = result;        while (n--) {
                memcpy(ptr, str, str_len);
                ptr += str_len;
        }
        *ptr = '/0';
        RETURN_STRINGL(result, result_length, 0);

}

7、test_extension目錄下執行:/usr/local/bin/phpize

Configuring for:
PHP Api Version:         
20121113Zend Module Api No:      
20121212Zend Extension Api No:   
220121212

8、設定:./configure –with-php-config=/usr/local/bin/php-config
9、編譯:make
10、安裝:make install
安裝完成後/usr/local/lib/php/extensions/no-debug-zts-20121212/下會產生test_extension.so

#11、修改php .in,加上:extension=test_extension.so

#

以上是PHP擴充開發-LINUX環境的程式碼實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn