±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:
$ cd ..
$ vi ext/my_module/config.m4
$ ./buildconf
$ ./configure --[with|enable]-my_module
$ make
$ ./php -f ext/my_module/my_module.php
$ vi ext/my_module/my_module.c
$ 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´¢µÄÖµ¡£
(´ýÐø)