PHP extension Hello Word

Source: Internet
Author: User
Tags php source code
First, download the source code.

First to download PHP source code, here I use PHP5.3.5

Open we can see ext this directory This directory is put all the extensions. In the directory we can see the Ext_skel script below I use this command to build the basic architecture of the extension.

Ii. creation of a basic framework

./ext_skel--extname=example

This command will generate a example directory in the EXT directory with the following files in the directory.

#在目录下有不少文件, testmodule.c and CONFIG.M4 are the most important example.c             #是我们扩展的主要文件config. M4                    #m4是一个宏解释工具, The makefilecredites #这个文件没什么太大的作用 used to build our extension                    is just used to append some additional information to your extension experimental           #这个文件只是标志说, this extension is experimental, So you don't have to worry about it. php_example.h    #这个是我们扩展的头文件tests/001.phpt           #这个也是个测试文件, but using unit tests, phase testing

Third, the realization.

1, modify the configuration file Config.m4, remove the previous dnl (DNL in the M4 File table notes)

VI Config.m4php_arg_enable (example, whether to ENABLE Laiwenhui support,[  --enable-laiwenhui           enable Laiwenhui Support] # Php_arg_with (testmodule, for Testmodule support,# Make sure that the comment is aligned:#--with-testmodule
  
   include Testmodule Support] # Here's with is a description, to enable this module, depending on some other modules, here we can temporarily ignore. # For example: module example, if dependent on APXS, you need: #/configure--WITH-APXS=/USR/LOCAL/APACHE/BIN/APXS  --enable-example
  

2. Declaring functions

Editing in a file Php_example.h file

VI php_example.h

We found Php_function (confirm_example_compiled); Add php_function (test) behind it;

3. Implement function test

VI example.c

Write the following code to the last side

/** * I added the first PHP extension * * */php_function (TEST) {    char *str = NULL;    char *arg = NULL;    int Arg_len;    int len;//    str = "Hello My first PHP extention! ^_^";
len = strlen (str);
Start to want to write this can have no effect, do not know why not. does not support strlen but can be compiled through.    if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "s", &arg, &arg_len) = = FAILURE) {        return;    }    len = spprintf (&str, 0, "Hello My first PHP extention! ^_^ param%s ", arg);    Return_stringl (str, len, 0);}
SPPRINTF should be a function of PHP.
Zend_parse_parameters is a function of PHP that gets the argument resolved if the failure is returned.

4. Tell the Zend engine what functions are in this example module.

Found: Zend_function_entry testmodule_functions[] This line, the comment means: all the available functions must be defined in this area.
Modified to:

* Every user visible function must has a entry in example_functions[]. */const zend_function_entry example_functions[] = {        Php_fe (confirm_example_compiled,        NULL)/           * for testing, remove later. *        /Php_fe (test,    null)/           * My extension Test */        {null,null, null}       


5. Compiling

Enter the example module

./configure? with-php-config=/usr/local/php/bin/php-config

/usr/local/php/bin/phpize

Make

CP modules/example.so/usr/local/php/lib/php/extensions/

Then modify the php.ini, let example.so enable, and finally restart the Web service

Four, test.

 
     

Here is a more clear explanation of the article of Bird brother.

Http://www.laruence.com/2009/04/28/719.html

  • 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.