Hello World: The first PHP extension _php tutorial

Source: Internet
Author: User
Tags php source code sapi

Goal
Create a PHP extension called Hello, and implement the only function inside the Hello_world, the function is to print out "Hello World" string.

Pre-conditions
A computer with a C compiler, PHP running environment, a text editor called hand.
Important: Do not try to write PHP extensions under Windows, Visual C, MinGW compiler is not good, I have been tinkering for one weeks have not been able to compile under Windows successfully. So at least in a Unix environment. Mac and various Linux environments are available.

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. Unzip to a directory, such as php5-5.3.5. In the source code directory, the EXT directory is where all the PHP extensions are located, and other directories are not considered for the time being.

Framework code to generate PHP extensions
In the Php5-5.3.5/ext directory, there is a file named Ext_skel, which is a handy tool for creating extensions. Make sure it has executable permissions (chmod u+x Ext_skel) and executes under terminal

./ext_skel--extname=hello

A Hello directory is created in the Ext directory, which is the initial skeleton code. The next task is to create a hello extension and implement the Hello_world function.

Edit CONFIG.M4
Opening the ext/hello/config.m4 with a text editor with a large number of comments (lines starting with DNL) has basically made a lot of sense. What we have to do here is to

DNL php_arg_enable (Hello, whether to ENABLE Hello Support,dnl make sure that's comment is ALIGNED:DNL [  --enable-hel Lo           Enable Hello support])

These three lines uncomment. This allows you to compile the extensions we just wrote with the./configure--enable-hello at the next compile time.

Regenerate Configure
Back to the source code root, run the./buildconf--force, which activates the Configure--enable-hello parameter. If you are running the following error in the buildconf times:

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 person usage)

sudo apt-get install autoconf2.13

Compiling extensions
The hello extension is now ready to compile, although the Hello_world function is not yet implemented. Compile it first to make sure there are no problems with the environment configuration.

./configure--enable-hellomake

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

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

Check the extension compilation. No surprises, you should be prompted.

Functions available in the test extension:confirm_hello_compiledcongratulations! You have successfully modified EXT/HELLO/CONFIG.M4. Module Hello is now compiled to PHP.

Writing Hello_world functions
declaring 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);

Added later

Php_function(hello_world);

That is, the prototype of the Hello_world function is declared in the extended header file. Php_function is a C-language macro used to define PHP functions. As for the appearance of macro expansion, almost no need to think about it. It only works.

implementation function : Open hello.c, add at the end of the file

Php_function(Hello_world){php_printf("Hello World");return;}

This is the implementation of the Hello_world function. The function of php_printf is to output a string to SAPI, similar to echo in the PHP language.

The next step is to register the Hello_world function with zend_module_entry so that the function becomes "visible" in the PHP program. Found it

ConstZend_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 to:

ConstZend_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[] * /};

The entire code for the Hello extension is now written. Finally, make a bit.

Test
Run Sapi/cli/php-r ' Hello_world () under the terminal, echo "\ n"; ', if you see the output "Hello World", it succeeds.

How to compile an extension into a. so file
The result of the compilation above is that the hello extension is compiled into the PHP core. If you want to compile into a. so extension for publishing. Need to use

./configure--enable-hello=sharedmake

This will generate the hello.so file in the modules directory when the compilation is complete. Copy it to the Extension_dir of your PHP run environment and use it like any other extension. Note that the PHP version is required. If you are compiling an extension in PHP 5.3.5 's source code environment, the resulting. So file can only be used in a PHP 5.3.5 runtime environment.

Finally, if you are interested in PHP extensions, you can look at the book "Extending and Embedding PHP", the author is still a MM. There is no Chinese version, English electronic version of their own search.

Excerpt from Lostwolf Blog

http://www.bkjia.com/PHPjc/478541.html www.bkjia.com true http://www.bkjia.com/PHPjc/478541.html techarticle The target creates a PHP extension called Hello, and implements the only function inside the Hello_world, which is to print out the Hello World string. Prerequisites 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.