HelloWorld: The first PHP Extension _ PHP Tutorial

Source: Internet
Author: User
Tags sapi
HelloWorld: The first PHP extension. The target creates a PHP extension named hello and implements the only function hello_world in it to print out the HelloWorld string. Prerequisite: a C compiler has been installed.

Target
Create a PHP extension named hello and implement the only function hello_world in it to print out the "Hello World" string.

Prerequisites
A computer that has installed the C compiler and PHP runtime environment, and a handy text editor.
Important: Do not try to write PHP extensions in Windows. Visual C and MinGW compilers are not easy to use. I have been playing tricks for more than a week and have not been able to compile successfully in Windows. So at least in a Unix environment. Mac and various Linux environments.

Download PHP source code
First use php-v to determine the PHP version on the system, and then download the corresponding source code package to php.net. Decompress it to a directory, such as a php5-5.3.5. In the source code directory, the ext directory is where all PHP extensions are located. other directories do not need to be considered for the moment.

Generate PHP extension framework code
Under the php5-5.3.5/ext Directory, there is a file named ext_skel, a simple tool for creating extensions. Make sure that it has the executable permission (chmod u + x ext_skel) and runs it on the terminal.

./ext_skel --extname=hello

A hello directory is created under the ext directory, which contains the initial skeleton code. The next task is to create a hello extension and implement the hello_world function.

Edit config. m4
Open ext/hello/config. m4 in a text editor, which contains a large number of comments (lines starting with dnl). many problems have been explained. What we need to do here is

dnl PHP_ARG_ENABLE(hello, whether to enable hello support,dnl Make sure that the comment is aligned:dnl [  --enable-hello           Enable hello support])

Uncomment these three lines. In this way, we can use./configure -- enable-hello to compile the extension we just wrote during the next compilation.

Regenerate configure
Return to the source code root directory and run./buildconf -- force. The configure -- enable-hello parameter is activated. If you encounter the following error when running buildconf:

buildconf: Your version of autoconf likely contains buggy cache code.           Running vcsclean for you.           To avoid this, install autoconf-2.13.

Please install autoconf-2.13 (ubuntu lazy usage)

sudo apt-get install autoconf2.13

Compilation extension
At this time, the hello extension can be compiled, although the hello_world function has not yet been implemented. Compile it first to make sure there are no environment configuration problems.

./configure --enable-hellomake

After a while (in fact, the entire PHP is also compiled ),

./sapi/cli/php -f ext/hello/hello.php

Check the extended compilation. Should prompt

Functions available in the test extension:confirm_hello_compiledCongratulations! You have successfully modified ext/hello/config.m4. Module hello is now compiled into PHP.

Compile the hello_world function
Declare a function: Open ext/hello/php_hello.h

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

Add

PHP_FUNCTION(hello_world);

That is, the prototype of the hello_world function is declared in the expanded header file. PHP_FUNCTION is a C-language macro used to define PHP functions. There is almost no need to think about the future of macro scale. Just use it.

Implementation functions: Open hello. c and add at the end of the file

PHP_FUNCTION(hello_world){php_printf("Hello World");return;}

Here is the implementation of the hello_world function. Php_printf outputs a string to SAPI, similar to echo in PHP.

Next, you need to register the hello_world function to zend_module_entry so that this function can become "visible" in the PHP program. Find

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

Modify it:

const zend_function_entry hello_functions[]={PHP_FE(confirm_hello_compiled,NULL)/* For testing, remove later. */PHP_FE(hello_world, NULL){NULL, NULL, NULL}/* Must be the last line in hello_functions[] */};

Now the whole hello extension code is compiled. Make again.

Test
Run sapi/cli/php-r 'Hello _ world (); echo "\ n"; 'on the terminal. if "hello World" is displayed, the operation is successful.

How to compile extensions into. so files
The above compilation result is to compile the hello extension into the PHP core. If you want to compile it into a. so extension for release. Need to use

./configure --enable-hello=sharedmake

After compilation, the hello. so file is generated in the modules directory. Copy it to extension_dir of your PHP runtime environment and use it like other extensions. Note the PHP version. If you compile the extension in the source code environment of PHP 5.3.5, the generated. so file can only be used in the runtime environment of PHP 5.3.5.

Finally, if you are interested in PHP extensions, you can read Extending and Embedding PHP. The author is still a MM. Currently, there is no Chinese version. you can search for the English version yourself.

From lostwolf blog

Secret creates a PHP extension named hello and implements the only function hello_world in it to print out the Hello World string. Prerequisite a C compiler has been installed...

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.