PHP extension development-01: the first extension

Source: Internet
Author: User

<HEAD>

The following operations are performed in Ubuntu 12.04 and the LAMP environment has been set up.

</HEAD>


1. Download PHP source code

1. Install GIT first

sudo apt-get install git  

2. Clone PHP source code


cd /git clone https://github.com/php/php-src.gitls 

The php-src folder is displayed.


3. Enter the ext directory


cd php-src/extls

You will see many extensions, such as curl and pdo, and the script ext_skel used to create the extension.



2. Create a skeleton to modify parameters

1. Use ext_skel to create a skeleton


./ext_skel --extname=yourname

Yourname: name of the extension you want to create. First, create one, for example, rube.

After the folder is created, the folder rube appears in the current folder.


cd rube

2. Modify the parameters of config and m4.



vim config.m4dnl Otherwise use enable: PHP_ARG_ENABLE(rube, whether to enable rube support,dnl Make sure that the comment is aligned: [  --enable-rube           Enable rube support])

Remove dnl before PHP_ARG_ENABLE (rube, whether to enable rube support and [-- enable-rube Enable rube support. Modify to as shown above



3. Compile php_rube.h and rube. c.

1. Edit php_rube.h.


vim php_rube.h


Add at the end of php_rube.h


PHP_FUNCTION(confirm_rube_compiled);PHP_FUNCTION(hello);

Hello, the function you want to create


2. Edit rube. c


vim rube.cconst zend_function_entry rube_functions[] = {    PHP_FE(confirm_rube_compiled,    NULL)          PHP_FE(hello,    NULL)             PHP_FE_END  };

Modify zend_function_entry rube_functions [] and add it after PHP_FE (confirm_rube_compiled, NULL ).



  PHP_FE(hello,    NULL) 


3. Compile Functions

Next, write the hello function. first, write a simple function that outputs "Hello my first extention...

Add at the end of rube. c


PHP_FUNCTION(hello){char *arg = "Hello my first extention!";int  len;char *strg;len = spprintf(&strg, 0, "%s\n", arg);RETURN_STRINGL(strg, len, 0);}

Save and exit




4. Compile the code

1. Compile the so file


cd /php-src/ext/rubewhereis phpize

Check whether phpize exists


If phpize is run


Sudo apt-get install php5-dev run phpize after installation


Then


./Configure -- with-php-config = Your php-config location

If the location of php-config cannot be found



whereis php-config



./Configure -- with-php-config = Your php-config location

Next



make

If an error occurs in your code during compilation, an error is reported.


Make

Build complete

Indicates that the compilation is successful.

make install


After installation, rube. the so file will be in the modules folder in the current folder, and will also be installed in the location prompted by the system (that is, the default installation location for php extension in your system ), my prompt is as follows:

Installing shared extensions:     /usr/lib/php5/20090626+lfs/



Indicates that rube. so is installed in the/usr/lib/php5/20090626 + lfs/directory.

Ls/usr/lib/php5/20090626 + lfs/# check whether it is in this folder


2. Modify php. ini

Find the php. ini file and add it at the end of the file.

Extension =/usr/lib/php5/20090626 + lfs/rube. so # my extensions can be modified in/usr/lib/php5/20090626 + lfs/rube. so.


Restart apache

5. Test

Create test. php In the root directory of your website

vim test.php<?php  echo hello();




Result:
Hello my first extention




This article is from the "Rube" blog, please be sure to keep this source http://hirube.blog.51cto.com/7190302/1287956

Related Article

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.