PHP擴充編寫樣本

來源:互聯網
上載者:User

標籤:

1、產生描述檔案,包含對函數等的定義

 

[[email protected] php-extension]$ cat hello_cy.def

string self_concat(string str, int n);

 

 

 

2、進入PHP源碼的ext目錄

 

 

[[email protected] ext]$ pwd

/home/chengyi/centos_soft_install/php-5.2.5/ext

 

 

3、查看ext_skel的協助,我們主要用到的是 --extname=module  定義將要產生的模組名稱; --proto=file    指向剛才的描述函數定義的檔案。

 

[[email protected] ext]$ ./ext_skel --help

./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]

           [--skel=dir] [--full-xml] [--no-help]

 

  --extname=module   module is the name of your extension

  --proto=file       file contains prototypes of functions to create

  --stubs=file       generate only function stubs in file

  --xml              generate xml documentation to be added to phpdoc-cvs

  --skel=dir         path to the skeleton directory

  --full-xml         generate xml documentation for a self-contained extension

                     (not yet implemented)

  --no-help          don‘t try to be nice and create comments in the code

                     and helper functions to test if the module compiled

 

4、執行ext_skel,產生空模組

 

 

[[email protected] ext]$ ./ext_skel --extname=hello_cy --proto=/home/chengyi/test-all/php-extension/hello_cy.def  

 

Creating directory hello_cy

awk: /home/chengyi/centos_soft_install/php-5.2.5/ext/skeleton/create_stubs:56: warning: escape sequence `/|‘ treated as plain `|‘

Creating basic files: config.m4 config.w32 .cvsignore hello_cy.c php_hello_cy.h CREDITS EXPERIMENTAL tests/001.phpt hello_cy.php [done].

 

To use your new extension, you will have to execute the following steps:

 

1.  $ cd ..

2.  $ vi ext/hello_cy/config.m4

3.  $ ./buildconf

4.  $ ./configure --[with|enable]-hello_cy

5.  $ make

6.  $ ./php -f ext/hello_cy/hello_cy.php

7.  $ vi ext/hello_cy/hello_cy.c

8.  $ make

 

Repeat steps 3-6 until you are satisfied with ext/hello_cy/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.

 

 

5、將產生的空模組mv到自己的目錄下去(這裡我們沒有按照上面ext_skel的說明來做)

 

[[email protected] ext]$ mv hello_cy/ ~/test-all/php-extension/ 

 

 

6、進入到空模組下,查看都產生了哪些檔案

 

 

 

[[email protected] hello_cy]$ ls -p

config.m4  config.w32  CREDITS  EXPERIMENTAL  hello_cy.c  hello_cy.php  php_hello_cy.h  tests/

 

 

可以看到,有一個tests目錄。

 

 

7、修改config.m4檔案,開啟編譯選項

 

 

PHP_ARG_ENABLE(hello_cy, whether to enable hello_cy support,

[  --enable-hello_cy           Enable hello_cy support])

 

 

if test "$PHP_HELLO_CY" != "no"; then

AC_DEFINE(HAVE_HELLO_CY, 1, [Whether you have Hello World])

 

PHP_NEW_EXTENSION(hello_cy, hello_cy.c, $ext_shared)

fi

 

 

8、執行phpize產生更多檔案,為編譯做準備

 

 

[[email protected] hello_cy]$ phpize 

Configuring for:

PHP Api Version:         20041225

Zend Module Api No:      20060613

Zend Extension Api No:   220060519

[[email protected] hello_cy]$ ls

acinclude.m4    build         config.m4   configure.in  EXPERIMENTAL  install-sh       missing         run-tests.php

aclocal.m4      config.guess  config.sub  config.w32    hello_cy.c    ltmain.sh        mkinstalldirs   tests

autom4te.cache  config.h.in   configure   CREDITS       hello_cy.php  Makefile.global  php_hello_cy.h

 

 

 

9、開始編譯

./configure --enable-hello_cy

make

make test

sudo make install (有時php ext路徑不對,就手工cp過去)

 

10、測試

 

 

[[email protected] hello_cy]$ php hello_cy.php 

Functions available in the test extension:

confirm_hello_cy_compiled

self_concat

 

Congratulations! You have successfully modified ext/hello_cy/config.m4. Module hello_cy is now compiled into PHP.

 

 

11、如果想要重新擷取到一個乾淨的目錄,執行 phpize --clean

 

 

12、將擴充載入,修改php.ini檔案

如果不確定php.ini的位置,執行如下:

 

 

[[email protected] hello_cy]$ php -i|grep "php.ini"

Configuration File (php.ini) Path => /usr/local/php/lib

Loaded Configuration File => /usr/local/php/lib/php.ini

 

 

可以看到,該檔案在/usr/local/php/lib/php.ini。

 

在該檔案中添加: extension=hello_cy.so

 

然後重啟apache,如果需要在web端執行的話(如果僅在本地指令碼執行,則不用重啟apache,因為壓根沒關係)。

 

 

-----------------------------------------------------

 

探求代碼為什麼這麼寫呢?

 

首先要聲明,探求這些只能去PHP的源碼看了。源碼中比較重要的兩個目錄是main和Zend。

 

 

1、PHP_FUNCTION(self_concat){} 是個什麼東西?為什麼要用它來定義我們的函數呢?

 

我們在裡面找尋PHP_FUNCTION的定義,在main/下:

php.h:#define PHP_FUNCTION                      ZEND_FUNCTION

 

然後我們去Zend/目錄下,發現了 ZEND_FUNCTION的定義:

zend_API.h:#define ZEND_FUNCTION(name)                          ZEND_NAMED_FUNCTION(ZEND_FN(name))

 

然後繼續由 ZEND_NAMED_FUNCTION, ZEND_FN的定義追查下去得到: void zif_name(INTERNAL_FUNCTION_PARAMETERS)

 

再看INTERNAL_FUNCTION_PARAMETERS (其實php源碼下都有tags,直接ctrl+]就可以追蹤定義了),得到完整的函數定義:

 

void zif_name(int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC); // 其實我還蠻好奇這幾個參數為什麼要這樣設定的,等有空去仔細看源碼

 

2、怎樣傳回值?

 

我們經常使用的傳回值的宏是 RETURN_STRING之類的,去Zend/下找它的定義:

#define RETURN_STRING(s, duplicate)     { RETVAL_STRING(s, duplicate); return; }

#define RETVAL_STRING(s, duplicate)         ZVAL_STRING(return_value, s, duplicate)

 

可以看出,是給函數定義的參數return_value賦值了,然後估計是由PHP內部的機制,直接把return_value返回。

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.