PHP Development entry 2> PHP extension development Entry 2 HELLOWORLD

Source: Internet
Author: User
PHP Development entry 2 & gt; PHP extension development Entry 2 HELLOWORLD development PHP extension is a COOL thing. However, the difficulty is naturally much more complicated than developing a PHP program. C language.

I started learning programming as the dumbest way. Since I have not studied computer science, I have learned a lot about programming. Like most of my siblings, I also bought Tan Haoqiang's C language. of course, this is recommended for everyone, on the other hand, teacher Tan's books are placed in the most prominent position on the bookshelves of programming, and other versions are too difficult to buy in our small place.

Learning programming begins with copying code and deleting code. I think it doesn't matter if I start to understand this development extension. I should first clarify the structure of the writing process. This should be relatively simple, it is where, what code to write, what can be modified, and what cannot be modified. After getting familiar with the process, you need to understand what this code means, and also understand the role of this code in the process of deleting the code.

Today, I wrote a 'Hello World', which is the same as that of all programming languages. How to write a function that returns "hello world.

First, find the PHP source code directory in the terminal, and then enter the following content in the ext directory, where "helloworld" is the name of today's function

./ext_skel  --extname=helloworld

Let's witness the birth of a miracle. the ext Directory has the helloworld directory. This is an extension without any features. next we will add features for it.

Open the php_helloworld.h file and add the following code to it:

PHP_FUNCTION(hellworld);

The added file is as follows:

/* + Versions + | PHP Version 5 | + ------------------------------------------------------------------------ + | Copyright (c) 1997-2015 The PHP Group | + versions + | This source file is subject to version 3.01 of the PHP license, | that is bundled with this package in the file LICENSE, and is | available through the world-wide-web at the following url: | http://www.php.net/license/3_01.txt | If you did not receive a copy of the PHP license and are unable to | obtain it through the world-wide-web, please send a note to | license@php.net so we can mail you a copy immediately. | + dimensions + | Author: | + dimensions + * // * $ Id $ */# ifndef PHP_HELLOWORLD_H # define PHP_HELLOWORLD_Hextern zend_module_entry helloworld_module_entry; # define export & helloworld_module_entry # define PHP_HELLOWORLD_VERSION "0.1.0"/* Replace with version number for your extension */# ifdef PHP_WIN32 # define PHP_HELLOWORLD_API _ declspec (dllexport) # elif defined (_ GNUC _) & _ GNUC _> = 4 # define PHP_HELLOWORLD_API _ attribute _ (visibility ("default "))) # else # define PHP_HELLOWORLD_API # endif # ifdef ZTS # include "TSRM. h "# login (helloworld); PHP_MSHUTDOWN_FUNCTION (helloworld); PHP_RINIT_FUNCTION (helloworld); login (helloworld); PHP_MINFO_FUNCTION (helloworld); PHP_FUNCTION (login);/* For testing, remove later. * // restart start ------------------------------------------------- // Here is the beginning of the content I added PHP_FUNCTION (hellworld ); // end here // specify END tables/* Declare any global variables you may need between the BEGINand end macros here: upper (helloworld) long global_value; char * global_string; ZEND_END_MODULE_GLOBALS (helloworld) * // * In every utility function you add that needs to use variables in php_helloworld_globals, call TSRMLS_FETCH (); after declaring other variables used by that function, or better yet, pass in TSRMLS_CC after the last function argument and declare your utility function with TSRMLS_DC after the last declared argument. always refer to the globals in your function as HELLOWORLD_G (variable ). you are encouraged to rename these macros something shorter, see examples in any other php module directory. */# ifdef ZTS # define HELLOWORLD_G (v) TSRMG (helloworld_globals_id, zend_helloworld_globals *, v) # else # define HELLOWORLD_G (v) (helloworld_globals.v) # endif/* PHP_HELLOWORLD_H * // ** Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw = 4 ts = 4 OFDM = marker * vim <600: noet sw = 4 ts = 4 */

Open helloworld. c. The modification is as follows:

/* + Versions + | PHP Version 5 | + ------------------------------------------------------------------------ + | Copyright (c) 1997-2015 The PHP Group | + versions + | This source file is subject to version 3.01 of the PHP license, | that is bundled with this package in the file LICENSE, and is | available through the world-wide-web at the following url: | http://www.php.net/license/3_01.txt | If you did not receive a copy of the PHP license and are unable to | obtain it through the world-wide-web, please send a note to | license@php.net so we can mail you a copy immediately. | + outputs + | Author: | + ---------------------------------------------------------------------- + * // * $ Id $ */# ifdef HAVE_CONFIG_H # include "config". H "# endif # include" php. h "# include" php_ini.h "# include" ext/standard/info. h "# include" php_helloworld.h "/* If you declare any globals in php_helloworld.h uncomment this: ZEND_DECLARE_MODULE_GLOBALS (helloworld) * // * True global resources-no need for thread safety here */static int le_helloworld; /* {helloworld_functions [] ** Every user visible function must have an entry in helloworld_functions []. */Const zend_function_entry helloworld_functions [] = {PHP_FE (confirm_helloworld_compiled, NULL)/* For testing, remove later. */PHP_FE (helloworld, NULL)/* Add code here, same as the function name in the previous file */PHP_FE_END/* Must be the last line in helloworld_functions [] */}; /* }}* // * {{{ helloworld_module_entry */zend_module_entry helloworld_module_entry ={# if ZEND_MODULE_API_NO> = require, # endif "hellow Orld ", helloworld_functions, PHP_MINIT (helloworld), PHP_MSHUTDOWN (helloworld), PHP_RINIT (helloworld ), /* Replace with NULL if there's nothing to do at request start */PHP_RSHUTDOWN (helloworld ), /* Replace with NULL if there's nothing to do at request end */PHP_MINFO (helloworld), # if ZEND_MODULE_API_NO> = 20010901PHP_HELLOWORLD_VERSION, # endifSTANDARD_MODULE_PROPERTIES }; /* }}*/# ifdef COMPILE_DL_HELLOWORL DZEND_GET_MODULE (helloworld) # endif/* {PHP_INI * // * Remove comments and fill if you need to have entries in php. iniPHP_INI_BEGIN () STD_PHP_INI_ENTRY ("helloworld. global_value "," 42 ", PHP_INI_ALL, OnUpdateLong, global_value, zend_helloworld_globals, helloworld_globals) STD_PHP_INI_ENTRY (" helloworld. global_string "," foobar ", PHP_INI_ALL, OnUpdateString, global_string, zend_helloworld_globals, hello World_globals) PHP_INI_END () * // *} * // * {response * // * Uncomment this function if you have INI entriesstatic void equals (zend_helloworld_globals * helloworld_globals) {helloworld_globals-> global_value = 0; helloworld_globals-> global_string = NULL;} * // * }}* // * {PHP_MINIT_FUNCTION */PHP_MINIT_FUNCTION (helloworld) {/* If you have INI entries, uncomment Se lines REGISTER_INI_ENTRIES (); */return SUCCESS;}/* }}* // * {PHP_MSHUTDOWN_FUNCTION */PHP_MSHUTDOWN_FUNCTION (helloworld) {/* uncomment this line if you have INI entriesUNREGISTER_INI_ENTRIES (); */return SUCCESS ;} /* }}* // * Remove if there's nothing to do at request start * // * {PHP_RINIT_FUNCTION */PHP_RINIT_FUNCTION (helloworld) {return SUCCESS ;} /* }}* // * Remove if there's nothing to do Request end * // * {PHP_RSHUTDOWN_FUNCTION */PHP_RSHUTDOWN_FUNCTION (helloworld) {return SUCCESS;}/* }}* // * {PHP_MINFO_FUNCTION */PHP_MINFO_FUNCTION (helloworld) {php_info_print_table_start (); php_info_print_table_header (2, "helloworld support", "enabled"); php_info_print_table_end ();/* Remove comments if you have entries in php. iniDISPLAY_INI_ENTRIES (); * //}/*} * // * Remove the following function When you have successfully modified config. m4 so that your module can be compiled into PHP, it exists only for testing purposes. * // * Every user-visible function in PHP shoshould document itself in the source * // * {proto string confirm_helloworld_compiled (string arg) return a string to confirm that the module is compiled in */PHP_FUNCTION (confirm_helloworld_compiled) {char * arg = NULL; int arg_len, Len; char * strg; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", & arg, & arg_len) = FAILURE) {return;} len = spprintf (& strg, 0, "Congratulations! You have successfully modified ext/%. 78 s/config. m4. Module %. 78 s is now compiled into PHP. "," helloworld ", arg); RETURN_STRINGL (strg, len, 0);}/* }}* // * The previous line is meant for vim and emacs, so it can correctly fold and unfold functions in source code. see the corresponding marks just before function definition, where the functions purpose is also incluented. please follow this convention For the convenience of others editing your code. * // ** Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw = 4 ts = 4 OFDM = marker * vim <600: noet sw = 4 ts = 4 * // * the function does not receive parameters but only returns values. */PHP_FUNCTION (helloworld) {int len; char * strg; len = spprintf (& strg, 0, "%. 78 s "," helloworld "); RETURN_STRINGL (strg, len, 0 );}

Before compilation, you also need to modify the file config. m4 to delete the dnl at the beginning of lines 10, 11, and 12. Delete the comments here, which is used to compile the extension during compilation, otherwise it will be ignored.

dnl $Id$dnl config.m4 for extension helloworlddnl Comments in this file start with the string 'dnl'.dnl Remove where necessary. This file will not workdnl without editing.dnl If your extension references something external, use with:PHP_ARG_WITH(helloworld, for helloworld support,Make sure that the comment is aligned:[  --with-helloworld             Include helloworld support])dnl Otherwise use enable:dnl PHP_ARG_ENABLE(helloworld, whether to enable helloworld support,dnl Make sure that the comment is aligned:dnl [  --enable-helloworld           Enable helloworld support])if test "$PHP_HELLOWORLD" != "no"; then  dnl Write more examples of tests here...  dnl # --with-helloworld -> check with-path  dnl SEARCH_PATH="/usr/local /usr"     # you might want to change this  dnl SEARCH_FOR="/include/helloworld.h"  # you most likely want to change this  dnl if test -r $PHP_HELLOWORLD/$SEARCH_FOR; then # path given as parameter  dnl   HELLOWORLD_DIR=$PHP_HELLOWORLD  dnl else # search default path list  dnl   AC_MSG_CHECKING([for helloworld files in default path])  dnl   for i in $SEARCH_PATH ; do  dnl     if test -r $i/$SEARCH_FOR; then  dnl       HELLOWORLD_DIR=$i  dnl       AC_MSG_RESULT(found in $i)  dnl     fi  dnl   done  dnl fi  dnl  dnl if test -z "$HELLOWORLD_DIR"; then  dnl   AC_MSG_RESULT([not found])  dnl   AC_MSG_ERROR([Please reinstall the helloworld distribution])  dnl fi  dnl # --with-helloworld -> add include path  dnl PHP_ADD_INCLUDE($HELLOWORLD_DIR/include)  dnl # --with-helloworld -> check for lib and symbol presence  dnl LIBNAME=helloworld # you may want to change this  dnl LIBSYMBOL=helloworld # you most likely want to change this   dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,  dnl [  dnl   PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $HELLOWORLD_DIR/$PHP_LIBDIR, HELLOWORLD_SHARED_LIBADD)  dnl   AC_DEFINE(HAVE_HELLOWORLDLIB,1,[ ])  dnl ],[  dnl   AC_MSG_ERROR([wrong helloworld lib version or lib not found])  dnl ],[  dnl   -L$HELLOWORLD_DIR/$PHP_LIBDIR -lm  dnl ])  dnl  dnl PHP_SUBST(HELLOWORLD_SHARED_LIBADD)  PHP_NEW_EXTENSION(helloworld, helloworld.c, $ext_shared)fi

This is a success. The development tool has been completed, and the rest is compilation. In the extension directory, execute the following commands in sequence:

phpize
./configure --withphp-config=php-config
make
sudo make install

After installation, you need to enable the extension in the PHP. ini file and add

extension=helloworld.so

Let's witness the birth of a miracle.

    

This is the first PHP extension.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.