Home >php教程 >php手册 >Make a HelloWorld with PHP extension!

Make a HelloWorld with PHP extension!

WBOY
WBOYOriginal
2016-07-11 20:00:461414browse

Although PHP provides a large number of useful functions, extension programming may be required under special circumstances. For example, a large number of PECL (PHP Extension Community Library) are provided in the form of extensions (dynamic link library dll files ), they run much more efficiently than PEAR.
PHP extension is written in C or C and needs to be compiled into a dynamic link library dll file and registered in the PHP environment before it can be used.
Software requirements for writing PHP extensions:
​​ VC 6.0 or VC .NET environment.
The source code of PHP needs to be compiled.
If you are not willing to compile the source code of PHP, you can download the successfully compiled binary code of PHP (that is, the file packages we use to deploy the PHP runtime environment). Note that the versions of the source file package and compiled package downloaded separately must be consistent.

Process:

1. Install VC 6.0 and choose to add its executable file path to the environment variable so that the compiler can be run in any path in the command line environment.
2. Install the PHP running environment and correctly integrate it with IIS. Assuming that the PHP version used is 5.2.5, download the php-5.2.5-Win32.zip binary package and php-5.2.5.tar.gz source code package. The installation environment is C:php-5.2.5-Win32. Extract the source code package and binary package to this folder respectively. Generate a php.ini file from php.ini-recommended copy.
3. Create the C:php-5.2.5-Win32Release_TS folder and copy the C:php-5.2.5-Win32devphp5ts.lib file here.
4. Enter the C:php-5.2.5-Win32ext folder and run the command:
C:php-5.2.5-Win32ext>..php.exe ext_skel_win32.php --extname=myphpext
Creating directory myphpext
Creating basic files: config.m4 config.w32 .cvsignore myphpext.c php_myphpext.h
CREDITS EXPERIMENTAL tests/001.phpt myphpext.php [done].

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

1. $ cd ..
2. $ vi ext/myphpext/config.m4
3. $ ./buildconf
​​ 4. $ ./configure --[with|enable]-myphpext
5. $ make
​​ 6. $ ./php -f ext/myphpext/myphpext.php
7. $ vi ext/myphpext/myphpext.c
8. $ make

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

As a result, a folder myphpext is generated under ext, which contains a PHP extension application programming framework. myphpext can be named arbitrarily. The format of the dll file generated in the future is php_[extname].dll. What we generate is php_myphpext.dll. The prompt information 1.2...8 of the running results is mainly for the Linux/Unix environment, and we do not need to pay attention to it. In fact, the config.m4 file may also need to be modified under Windows, but it is not needed for our simple framework yet.

The folder myphpext contains several files, including:

myphpext.dsp is the project file and will be used later;
Myphpext.php extension test file;
php_myphpext.h extended function definition header file
Myphpext.c extension function specific implementation

The above 2 important file contents:

php_myphpext.h file:

/*
       ----------------------------------------------------------------------
      | PHP Version 5                                                        |
       ----------------------------------------------------------------------
      | Copyright (c) 1997-2007 The PHP Group                                |
       ----------------------------------------------------------------------
      | 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.               |
       ----------------------------------------------------------------------
      | Author:                                                              |
       ----------------------------------------------------------------------
    
*/


    
/* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */

    #ifndef PHP_MYPHPEXT_H
    
#define PHP_MYPHPEXT_H

    
extern zend_module_entry myphpext_module_entry;
    
#define phpext_myphpext_ptr &myphpext_module_entry

    #ifdef PHP_WIN32
    
#define PHP_MYPHPEXT_API __declspec(dllexport)
    
#else
    
#define PHP_MYPHPEXT_API
    
#endif

    #ifdef ZTS
    #include 
"TSRM.h"
    
#endif

    PHP_MINIT_FUNCTION(myphpext);
    PHP_MSHUTDOWN_FUNCTION(myphpext);
    PHP_RINIT_FUNCTION(myphpext);
    PHP_RSHUTDOWN_FUNCTION(myphpext);
    PHP_MINFO_FUNCTION(myphpext);

    PHP_FUNCTION(confirm_myphpext_compiled); 
/* For testing, remove later. */
    PHP_FUNCTION(HelloPHP);

    
/*
       Declare any global variables you may need between the BEGIN
     and END macros here:

    ZEND_BEGIN_MODULE_GLOBALS(myphpext)
     long  global_value;
     char *global_string;
    ZEND_END_MODULE_GLOBALS(myphpext)
    
*/


    
/* In every utility function you add that needs to use variables
       in php_myphpext_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 MYPHPEXT_G(variable).  You are
       encouraged to rename these macros something shorter, see
       examples in any other php module directory.
    
*/


    #ifdef ZTS
    
#define MYPHPEXT_G(v) TSRMG(myphpext_globals_id, zend_myphpext_globals *, v)
    
#else
    
#define MYPHPEXT_G(v) (myphpext_globals.v)
    
#endif

    
#endif /* PHP_MYPHPEXT_H */

    
/*
     * Local variables:
     * tab-width: 4
     * c-basic-offset: 4
     * End:
     * vim600: noet sw=4 ts=4 fdm=marker
     * vim     
*/


myphpext.c file:

    

/*
       ----------------------------------------------------------------------
      | PHP Version 5                                                        |
       ----------------------------------------------------------------------
      | Copyright (c) 1997-2007 The PHP Group                                |
       ----------------------------------------------------------------------
      | 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.               |
       ----------------------------------------------------------------------
      | Author:                                                              |
       ----------------------------------------------------------------------
    
*/


    
/* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */

    #ifdef HAVE_CONFIG_H
    #include 
"config.h"
    
#endif

    #include 
"php.h"
    #include 
"php_ini.h"
    #include 
"ext/standard/info.h"
    #include 
"php_myphpext.h"

    
/* If you declare any globals in php_myphpext.h uncomment this:
    ZEND_DECLARE_MODULE_GLOBALS(myphpext)
    
*/


    
/* True global resources - no need for thread safety here */
    
static int le_myphpext;

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

    zend_function_entry myphpext_functions[] 
= {
     PHP_FE(confirm_myphpext_compiled, NULL)  
/* For testing, remove later. */
     PHP_FE(HelloPHP, NULL)
     
{NULL, NULL, NULL} /* Must be the last line in myphpext_functions[] */
    }
;
    
/* }}} */

    
/* {{{ myphpext_module_entry
     
*/

    zend_module_entry myphpext_module_entry 
= {
    
#if ZEND_MODULE_API_NO >= 20010901
     STANDARD_MODULE_HEADER,
    
#endif
     
"myphpext",
     myphpext_functions,
     PHP_MINIT(myphpext),
     PHP_MSHUTDOWN(myphpext),
     PHP_RINIT(myphpext),  
/* Replace with NULL if there's nothing to do at request start */
     PHP_RSHUTDOWN(myphpext), 
/* Replace with NULL if there's nothing to do at request end */
     PHP_MINFO(myphpext),
    
#if ZEND_MODULE_API_NO >= 20010901
     
"0.1"/* Replace with version number for your extension */
    
#endif
     STANDARD_MODULE_PROPERTIES
    }
;
    
/* }}} */

    #ifdef COMPILE_DL_MYPHPEXT
    ZEND_GET_MODULE(myphpext)
    
#endif

    
/* {{{ PHP_INI
     
*/

    
/* Remove comments and fill if you need to have entries in php.ini
    PHP_INI_BEGIN()
Make a HelloWorld with PHP extension! Make a HelloWorld with PHP extension!     PHP_INI_END()
    
*/

    
/* }}} */

    
/* {{{ php_myphpext_init_globals
     
*/

    
/* Uncomment this function if you have INI entries
    static void php_myphpext_init_globals(zend_myphpext_globals *myphpext_globals)
    {
     myphpext_globals->global_value = 0;
     myphpext_globals->global_string = NULL;
    }
    
*/

    
/* }}} */

    
/* {{{ PHP_MINIT_FUNCTION
     
*/

    PHP_MINIT_FUNCTION(myphpext)
    
{
     
/* If you have INI entries, uncomment these lines
     REGISTER_INI_ENTRIES();
     
*/

     
return SUCCESS;
    }

    
/* }}} */

    
/* {{{ PHP_MSHUTDOWN_FUNCTION
     
*/

    PHP_MSHUTDOWN_FUNCTION(myphpext)
    
{
     

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn