Php extension and embedding-c extension development helloworld_PHP tutorial

Source: Internet
Author: User
Php extension and embedding-c extension development helloworld. After the LAMP configuration environment is completed in linux, you can perform php extension development. Extension development in php is under the ext folder of the source code package. we can see that the LAMP configuration environment has been completed under linux, and php extension development can be performed.

Extension development in php is under the/ext folder of the source code package. we can see that there are already many well-developed extensions. For example, mysql and xml processing modules related to databases.

First, create a folder:

Mkdir hello

After entering this folder, create and open a configuration file:

Vim config. m4

Here is an example of configuration problems:

1 PHP_ARG_ENABLE(sample, whether to enable SAMPLE support,2 [ --enable-sample Enable SAMPLE support])3 if test "$PHP_SAMPLE" = "yes"; then4    AC_DEFINE(SAMPLE, 1, [Whether you have SAMPLE])5   PHP_NEW_EXTENSION(sample, sample.c, $ext_shared)6 fi
* This configuration file creates a -- enable-hello configuration option, and the second option of PHP_ARG_ENABLE is displayed during configuration.
* The third parameter of PHP_ARG_ENABLE is displayed when./configurehelp is called.
* Why sometimes enable-xxx and with-xxx are used? Enable can be disabled, but with requires additional third-party libraries
* If -- enable-hello is configured, the $ PHP_HELLO parameter will be set to yes, and the following operations will be performed.
* PHP_NEW_EXTENSION is to declare all the required source files: PHP_NEW_EXTENSION (sample, sample. c sample2.c sample3.c, $ ext_shared)
* This is generally the case when the last parameter is building a shared module. $ Ext_shared

The following lists possible configuration options in the config file: * PHP_ARG_WITH or PHP_ARG_ENABLE specifies the working mode of the PHP Extension Module. The former means that no third-party library is required, and the latter is the opposite;
* PHP_REQUIRE_CXX is used to specify that the extension uses C ++;
* PHP_ADD_INCLUDE specifies the header file directory used by the PHP extension module;
* PHP_CHECK_LIBRARY specifies the PHP extension module PHP_ADD_LIBRARY_WITH_PATH definition and library connection error information;
* PHP_ADD_LIBRARY (stdc ++, "", EXTERN_NAME_LIBADD) is used to link the standard C ++ library to the extension
* PHP_SUBST (EXTERN_NAME_SHARED_LIBADD) indicates that the extension is compiled into a dynamic link library;
* PHP_NEW_EXTENSION is used to specify which source files should be compiled and separated by spaces;
Next we will look at the header file: php_sample.h
1? # Ifndef PHP_SAMPLE_H2/* prevents two accesses */3 # define PHP_SAMPLE_H4/* defines the extended nature */5 # define PHP_SAMPLE_EXTNAME "sample" 6 # define PHP_SAMPLE_EXTVER "1.0" 7 /* when building outside the php source code tree, introduce configuration options. when using the phpize tool, the */8 # ifdef HAVE_CONFIG_H9 # include "config. h "10 # endif11/* introduce the php Standard header file */12 # include" php. h "13 PHP_FUNCTION (hello_world); // declare the function 14/* in the extension to define the entry point symbol. zend will use */15 extern zend_module_entry sample_module_entry when loading this module; 16 # define phpext_sample_ptr & sample_module_entry17 # endif/* PHP_SAMPLE_H */
At last, pay attention to two points: * php. h must be introduced.
* The Declaration zend_module_entry is declared as extern, so when the extension is extension = .. Zend can be found through dlopen () and dlsym.


Finally, let's look at the source file sample. c:
# Include "php_sample.h" static function_entry php_sample_functions [] = {PHP_FE (sample_hello_world, NULL) // all functions in any extension must be declared here. Output function name to User space {NULL, NULL, NULL}; zend_module_entry sample_module_entry = {// create an entry # if ZEND_MODULE_API_NO> = 20010901 // This is a version number STANDARD_MODULE_HEADER, # endif PHP_SAMPLE_EXTNAME, php_sample_functions,/* Functions add php_function to Zend */NULL,/* MINIT */NULL,/* MSHUTDOWN */NULL, /* RINIT */NULL,/* RSHUTDOWN */NULL,/* MINFO */# if ZEND_MODULE_API_NO >= 20010901 PHP_SAMPLE_EXTVER ,# Endif STANDARD_MODULE_PROPERTIES}; # ifdef COMPILE_DL_SAMPLEZEND_GET_MODULE (sample) # endif // when the extension is dynamically loaded, add a reference for Zend. remember to add it. /* Real part of the function body */PHP_FUNCTION (sample_hello_world) {php_printf ("Hello World! \ N ");}
This is the content of the source code.
Next we need to generate the extension: phpize. /configure -- enable-samplemakesudo make install. ini: extension = sample. so, restart apachesudo/etc/init. d/httpd restart
Next, on the phpinfo page, check whether the extension sample already exists. if so, verify the extension in test. php below:
 
In addition to "Hello World! ", It indicates that the php extension development is successful.

Bytes. Extension development in php is under the/ext folder of the source code package. you can see that there is already...

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.