PHP extension Development (a basic introduction and expansion of the basic steps of development)

Source: Internet
Author: User
Tags install php pear php framework php source code zend

Need to develop an extended scenario:

1 application is very efficient, such as complex image algorithm, need to be written into the expansion module

2 Some system calls can not be directly accessed with PHP, such as the fork function under Linux to create a process, you need to write an extension

3 need to commercialize an application, do not want to expose the source code


One, build PHP framework

1 first understand the source directory of PHP

Ext: A directory of dynamic and built-in modules where all official extensions can be found and new extensions will be placed here later

Main: Contains the key macro definitions for PHP

Pear:php's extension and application Library directory, containing Pear's core files

SAPI: Code that contains different server abstraction layers

tsrm:zned and PHP Thread-safe Explorer Directory

Zend: All files containing the Zend engine can be found with all Zend API definitions

In addition to the top of the file directory, the general will also include the following files come in:

Main/php.h: In the main directory, contains most of the PHP macros and PHP API definition

Zend/zend.h: The definition of the Zend macros and Zend APIs that are contained in the Zend directory

Zend/zend_api.h: Located in the Zend directory, contains the definition of the Zend API


2 Ext_skel Tools:

In the Ext source directory, there are Ext_skel and ext_skel_win32.h two automated build Extension development framework tools, the former used under Linux, the latter under win use

Win under the expansion of the development of the introduction:

1 using the Ext_skel tool to write an extension

A) CD to the Ext directory of the PHP source directory, enter the following command to extend the framework:

php ext_skel_win32.php--extname = Myext (if you are prompted that PHP is not an internal or external command, not a running program or batch file, please install PHP first and add PHP to the environment variable)

b) After the successful installation will find that the Ext directory more than a myext directory, Myext directory, MYEXT.DSP is VC + + project documents

c) compiling the installation extensions

With VC + + Open MYEXT.DSP Engineering files, press the Compile button to start compiling this extension, in the process of compiling, you will find the compiler Error prompts: Link fatal error:can ' tfind input Files "Php5ts.lib", from the installation of PHP Record (note is not the PHP source directory, is the installation directory) of the dev directory to copy Php5ts.lib to the created extended directory (myext) directory, press the Compile button again, this time can be successfully compiled

d The source code after the root directory will be more than a release_ts directory, in the directory has just compiled the dynamic extension file Php_myext.dll, if you can not find the Release_ts directory, and found the Debug_ts directory, please put the VC + + The compilation mode is changed into release mode

To this has compiled a php extension file, as long as the compiled Php_myext.dll placed in the installation directory of the Ext directory, in the php.ini add extension = Php_myext.dll and restart the Web server can be, After the installation is complete, you can see the extended information in the phpinfo like other extensions, and you can call the extended encapsulated function to confirm that the installation was successful

Introduction to Extended development under Linux (Take Ubutun system as an example):

A) installation of Php-dev packages

The use of the Phpize tool in the Php-dev package can reduce a lot of tedious steps, if compiled with PHP source code, you can not install the Php-dev package, the source code has been saturated phpize tools, under the Ubutun installation Dev Package command is sudo spt-get After the install Php5-dev installation is complete, you can use the following command to see if the installation is successful phpize--version, and if the Phpize version information is displayed it will prove the installation was successful

b using the Ext_skel tool, there is a Ext_skel file in the PHP source directory, similar to the ext_skel_win32.h under win

Eg: Create a php extension

./ext_skel--extname=myext then you can see a series of prompt steps

c) compiling the installation extensions

After you build the extension, go to the extended directory, use the Phpize command to generate the extended Configuration tool, and then compile and install:

CD Myext

Phpize

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

Make

Maketest

Make install

After the compilation is complete, you can see the resulting extension file under/usr/local/php5/lib/php/extensions/no_debug_non_.../myext.so

Then add the extended information in the phpini file extension = myext.so

d) using Php-m | grep Myext to see if the extension was installed successfully










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.