±àд×Ô¼ºµÄphpÀ©Õ¹º¯Êý

5年以前  |  阅读数:796 次  |  编程语言:PHP 

±aд×Ô¼ºµÄphpÀ(C)Õ¹º¯Êý php³ÌÐoдµÄʱ¼a³¤ÁË£¬×ÔÈ»¶ÔËuËuÌa¹(C)µÄ¹¦ÄÜÁËÈçÖ¸ÕÆ£¬ËuËuÌa¹(C)µÄÒ»´o¶Ñ¹¦ÄÜ£¬ÕaeÊǾoµÃºÜºÃÓ㬵«ÓÐʱºo»a¢ÏÖphpҲȱÉÙÒ»Ð(C)¹¦ÄÜ£¬×Ô¼º×ÜÊÇ»a²uÉuΪphpÌi¼ÓÒ»Ð(C)×Ô¶¨ÒaµÄ¹¦ÄܵÄÏe¨¡£¾Ã¶ø¾ÃÖ®£¬ÖÕÓÚ½ñÌi±i²»×¡ÁË£¬¿ªÊ¼¶¯ÊÖÑо¿ÈçºÎÌi¼Ó¡£

ÏÂÔØÒ»¸ophpµÄÔ´´uÂe°u£¬ÕaÀiʹÓõÄÊÇphp 4.0.5°ae£¬½aѹºo»a¿´µ½phpµÄ¸uĿ¼Ï»aÓÐREADME.EXT_SKELÕaÑuÒ»¸oÎļþ£¬´o¿ªÏeϸÔĶÁÁËһϣ¬¢ÏÖÁËÒ»¸odz£ºÃÓõŤ¾ß£¬Õa¸o¹¤¾ß¿ÉÒÔ°iÄa¹¹½¨Ò»¸o¿ÕµÄphpÀ(C)Õ¹£¬È»ºoÄaÏoÀiÃaeÌi¼ÓÏaÓ¦µÄ´uÂe¾Í¿ÉÒÔÍe³ÉÄa×Ô¼ºµÄ¹¦ÄÜÀ(C)Õ¹ÁË¡£ÏÂÃaeÎÒÃǾÍÀ´½eÉÜÈçºÎʹÓÃÕa¸o¹¤¾ß¡£

Ê×ÏÈתÒÆÄaµÄĿ¼µ½phpµÄĿ¼ÏµÄextĿ¼£¬Èç¹uÄaÖ»ÐeÒªÒ»¸o»u±¾µÄÀ(C)Õ¹¿o¼ÜµÄ»°£¬Ö´ÐÐÏÂÃaeµÄÃuÁi£º

./ext_skel --extname=module_name

module_nameÊÇÄa×Ô¼º¿ÉÒÔÑ¡ÔñµÄÀ(C)չģ¿eµÄÃu×Ö£¬ÀýÈçÎÒÑ¡ÔñµÄmy_module¡£Ö´Ðй¤¾ßºo»a×Ô¶¯ÔÚextĿ¼Ï½¨Á¢ÄaÑ¡ÔñµÄmodule_nameÃu×ÖµÄĿ¼£¬ÀiÃaeÒѾ­Éu³ÉÁËÏa¹ØµÄ´uÂe£¬ÕaÐ(C)´uÂeÖÐÖ»ÐeÒªµ÷Õuconfig.m4ÎļþÖеÄÈýÐÐ×¢Ê;ͿÉÒÔÕý³£µÄ±aÒe´øÕa¸o×Ô¶¨ÒaÀ(C)չģ¿eµÄphpÁË¡£ÔÚphpµÄ¸uĿ¼ִÐÐÏÂÁвÙ×÷¾Í¿ÉÒԵõ½¡£

./buildconf

./configure --enable-module_name

make

ÏÂÃaeÎÒÀ´ÑÝʾ½¨Á¢my_moduleÀ(C)Õ¹¿o¼ÜµÄÈ«¹ý³Ì£¬ÎªÁ˸uÓÐЧ¹u£¬ÎÒÃÇÀ´Íe³ÉÒ»¸ophpµÄÀ(C)Õ¹¹¦ÄÜ£¬ÔÚphpÖе÷ÓÃÕa¸o¹¦ÄÜ¿ÉÒÔÔÚwebÒ³ÃaeÖÐÏÔʾhello worldÕa¸o¾­µaµ¥´Ê¡£

ÔÚphpĿ¼ÏµÄextĿ¼ÖУ¬Ö´ÐÐÏÂÃaeµÄÃuÁi

./ext_skel --extname=my_module

µÃµ½*´À¡½a¹u£º

Creating directory my_module

Creating basic files: config.m4 Makefile.in .cvsignore my_module.c php_my_module.h tests/001.phpt my_module.php [done].

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

  1. $ cd ..

  2. $ vi ext/my_module/config.m4

  3. $ ./buildconf

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

  5. $ make

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

  7. $ vi ext/my_module/my_module.c

  8. $ make

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

Èç¹uÄaÄÜ¿´¶®ÉÏÃaeµÄ¶«Î÷£¬ÄǾÍÕÕ×ÅÈ¥×o¡£Èç¹u²»ÊÇÌ«Ã÷°×µÄ»°£¬°´ÕÕÎÒÏÂÃaeµÄÌaʾÀ´×oÒ²¿ÉÒÔ¡£

Cd my_module

Ê×ÏȽøÈemy_moduleĿ¼

vi config.m4

ʹÓÃÎı¾±a¼­Æ÷´o¿ªconfig.m4Îļþ£¬ÎļþÄÚÈÝ´oÖÂÈçÏ£º

dnl $Id$

dnl config.m4 for extension my_module

dnl don't forget to call PHP_EXTENSION(my_module)

dnl Comments in this file start with the string 'dnl'.

dnl Remove where necessary. This file will not work

dnl without editing.

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [ --with-my_module Include my_module support])

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [ --enable-my_module Enable my_module support])

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

dnl If you will not be testing anything external, like existence of

dnl headers, libraries or functions in them, just uncomment the

dnl following line and you are ready to go.

dnl Write more examples of tests here...

PHP_EXTENSION(my_module, $ext_shared)

Fi

¸u¾ÝÄa×Ô¼ºµÄÑ¡Ôñ½«

dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [ --with-my_module Include my_module support])

Ð޸ijÉ

PHP_ARG_WITH(my_module, for my_module support,

Make sure that the comment is aligned:

[ --with-my_module Include my_module support])

»oÕß½«

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [ --enable-my_module Enable my_module support])

Ð޸ijÉ

PHP_ARG_ENABLE(my_module, whether to enable my_module support,

Make sure that the comment is aligned:

[ --enable-my_module Enable my_module support])

Ò»°aÎÒ»aÑ¡ÔñºoÕߣ¬È»ºo±£´aeÍ˳o¡£Èç¹uÄa¶ÔviÎı¾±a¼­Æ÷µÄ²Ù×÷ÓÐÀ§ÄѵĻ°£¬Çe²Î¿¼ÏaÓ¦µÄ˵Ã÷ÎÄÕ£¬ÕaÀi¾Í²»ÔÙÏeϸÃeÊoÁË¡£

Vi my_module.c

½«ÎļþÆaÖеÄÏÂÁдuÂe½øÐÐÐÞ¸Ä

/* Every user visible function must have an entry in my_module_functions[].

*/

function_entry my_module_functions[] = {

    PHP_FE(say_hello,       NULL)  /* ßÌi¼Ó×ÅÒ»ÐдuÂe */  

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

    {NULL, NULL, NULL}      /* Must be the last line in my_module_functions[] */  

};

ÔÚÎļþµÄ×iºoÌi¼ÓÏÂÁдuÂe

PHP_FUNCTION(say_hello)

{

    zend_printf("hello world\n");  

}

±£´aeÎļþÍ˳o

vi php_my_module.h

ÔÚÎļþÖÐPHP_FUNCTION(confirm_my_module_compiled);Ò»ÐÐÇ°ÃaeÌi¼ÓÏÂÃaeµÄ´uÂe

PHP_FUNCTION(say_hello);

±£´aeÎļþÍ˳o

Í˻ص½phpµÄ¸uĿ¼Ï£¬Ö´ÐÐÏÂÃaeµÄÃuÁi

./buildconf

./configure --enable-my_module

make

Èç¹uÒ»ÇÐ˳ÀuµÄ»°£¬ÎÒÃÇÏÖÔÚÒѾ­½«À(C)չģ¿emy_module±aÒeµ½phpÀiÃaeÁË¡£ÎÒÃDZaдÏÂÃaeµÄ´uÂe½øÐвaÊÔ

<?

   Say_hello();  

?>

±£´aeÎļþΪsay_hello.php

ÔÚphpµÄ¸uĿ¼ÏÂÔËÐÐ

./php ¨Cq say_hello.php

Õý³£Çe¿oÏ»aÏÔʾ

hello world

±iʾÎÒÃǵĵÚÒ»¸oÀ(C)Õ¹Õý³£µÄÔËÐÐÁË£¡

½aÊÍÒ»ÏÂÉÏÃae×oµÄ²Ù×÷£¬ext_skelÉu³ÉÒ»Ð(C)¿oÏÂÎļþ£¬ÎÒÃÇÐeÒªÐÞ¸ÄÒÔÏÂÎļþ

my_module.c À(C)չģ¿eµÄÖ÷³ÌÐo

php_my_module.h À(C)չģ¿eµÄÍ*Îļþ

config.m4 ÅaÖÃÎļþ

Ö÷³ÌÐoÖÐÃeÊoÁËphpÀ(C)չģ¿eµÄÉuÃ÷£¬Ä£¿eÖк¬ÓжaÉÙ¸oº¯Êý£¬¸÷¸oº¯ÊýµÄ×÷Óã¬ÔÚphpinfoº¯ÊýÖÐÏÔʾʲôÄÚÈÝ£¬Ä£¿e³oʼ»¯×oÐ(C)ʲô£¬½aÊø×oÐ(C)ʲô¶¼»aÔÚÕa¸oÎļþÀi½øÐÐÃeÊo¡£ÎÒÃÇÔÚÉÏÃaeÖ»ÊÇÌi¼ÓÁËÒ»¸oº¯Êýsay_hello£¬²¢ÇÒÃeÊoÁËsay_helloº¯ÊýµÄ¾ßÌaÄÚÈÝ£¬µ÷ÓÃzend_printfϵͳº¯ÊýÔÚphpÖдoÓ¡×Ö*u´®¡£

ÔÚ¶ÔÓ¦µÄÍÎļþÖÐÉuÃ÷ÁËsay_helloÕa¸oº¯Êý£¬´Ó¶øÍe³ÉÁËÎÒÃÇÔ¤ÆڵŦÄÜ¡£ÏÂÃaeÎÒÃÇ»a±aдһ¸o¸u¸´ÔÓµÄÀ(C)Õ¹£¬´´ÔiÒ»¸o´ø²ÎÊýµÄphpÀ(C)Õ¹º¯Êý£¬¸u¾Ý¸øÈeµÄ²ÎÊý£¬ÏÔʾhello world, xxxx¡£Xxxx´u±iÊaÈeµÄ×Öu´®ÄÚÈÝ£¬ÀýÈçÎÒµÄÃu×Öyorgo¡£

Vi my_module.c

ÐÞ¸Ä×iºoµÄsay_helloº¯ÊýÄÚÈÝÈçÏ£º

PHP_FUNCTION(say_hello)

{

    zval **yourname;  



    if (ZEND_NUM_ARGS() != 1   

zend_get_parameters_ex(1, &yourname;) == FAILURE)

    {  

            WRONG_PARAM_COUNT;  

    }  



    zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));  

}

´aeÅÌÍ˳o¡£

ÍË»ØphpµÄ¸uĿ¼£¬ÔËÐÐ

make

ÐÞ¸Äsay_hello.phpΪ

<?

   Say_hello(¡°yorgo¡±);  

?>

±£´aeÍ˳oºoÔËÐÐ

./php ¨Cq say_hello.php

µÃ³o½a¹u

hello world, yorgo

±iʾÎÒÃÇÕa´ÎµÄÐÞ¸ÄÒ²³É¹¦ÁË£¬¿ÉÒԸıasay_helloÖеIJÎÊý£¬¿´¿´¶¯Ì¬µÄЧ¹u¡£

ÕaÀiÖ÷Òª½aÊÍÉÏÃaeÐ޸ĵĺ¯ÊýÄÚÈÝ£¬ÓÉÓÚsay_helloº¯ÊýÐeÒªÓвÎÊýÒýÈe£¬ËuÒÔÔÚmy_module.cÖеÄsay_helloº¯ÊýÖ÷ÒªÔÚ½øÐвÎÊýµÄ´¦Ài£¬½«phpÖÐÒýÓÃsay_helloʱËuÌiдµÄ²ÎÊýÄÚÈÝÕýÈ*µÄ´«µÝµ½my_module.cÖеÄsay_hello´¦Àiº¯ÊýÖС£Îª´Ë£¬³ÌÐoÖÐÌi¼ÓÁËÕaô¼¸ÐС£

zval **yourname;

if (ZEND_NUM_ARGS() != 1
zend_get_parameters_ex(1, &yourname;) == FAILURE)

{

WRONG_PARAM_COUNT;

}

zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));

´uÂe½aÊÍÈçÏ£º

zval **yourname;

³oʼ»¯Ò»¸o²ÎÊýµÄÖ¸Õe

ZEND_NUM_ARGS()

µÃµ½´«µÝ¹ýÀ´µÃ²ÎÊýÊýÁ¿£¬²¢ÇÒÅжÏÈç¹u²»Îª1µÄʱºo±iʾÓÐÎÊÌa£¬±¨´i¡£

zend_get_parameters_ex(1, &yourname;)

½«¸Õ¸Õ³oʼ»¯µÄÖ¸ÕeÖ¸Ïo´«µÝ¹ýÀ´µÄ²ÎÊý£¬Èç¹u²»³É¹¦Ôo±¨´i¡£

Z_STRVAL_PP(yourname)

´¦ÀiÖ¸ÕeÖ¸ÏoµÄ²ÎÊý²¢»ñµÃʵ¼Ê´ae´¢µÄÖµ¡£

(´ýÐø)

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例