How to write a standalone PHP extension

Source: Internet
Author: User
Tags install php mysql client php source code

Standalone PHP extensions can be distributed independently of the PHP source. To create an extension like this, you need two things ready:

Configuration file (CONFIG.M4)

The source code of your module

Let's describe what to do if you create these files and combine them.

Prepare the System tools.

To be able to build and run successfully on the system, you need to be ready to go to the following tools:

GNU autoconf

GNU Automake

GNU Libtool

GNU M4

All of these can be obtained from ftp://ftp.gnu.org/pub/gnu/.

Note: These are all tools that can be used in a Unix-like environment.

Retrofitting an already existing extension

To show that it's easy to create a separate extension, we'll start by changing an extension that's already embedded in PHP into a separate extension. Install PHP and execute the following command:

$ mkdir/tmp/newext
$ cd/tmp/newext

Now you have an empty directory. We copy the files from the MySQL extension directory:

$ CP-RP php-4.0.x/ext/mysql/*.
# Note: It seems that this README really needs to be updated.
# MySQL extensions have been removed from PHP7

Here the extension is complete, execution:

$ phpize

Now you can store the files in this directory independently anywhere, and this extension can be completely independent.

The user will need to use the following command at compile time:

$./configure
[--with-php-config=/path/to/php-config]
[--with-mysql=mysql-dir]
$ make Install

This allows the MySQL module to use the embedded MySQL client library or the installed MySQL directory in MySQL.

Note: meaning that you want to write a PHP extension, you need to have already installed PHP, you need to download a PHP source code.

Define a new extension

We named the example extension "Foobar".

The new extension contains two resource files: foo.c and bar.c (there are some header files, but these are not just important).

The example extension does not refer to any external libraries (this is important because the user does not need to specifically specify some compilation options).

The ltlibrary_sources option is used to specify the name of the resource file, and you can have any number of resource files.

Note: The configuration options in the Makefile.in file are described above, and you can refer to Xdebug.

Modify the configuration file for the M4 suffix

The M4 configuration file can specify some additional checks. For a standalone extension, you just need to make some macro calls.

Php_arg_enable (Foobar,whether to ENABLE Foobar,
[--enable-foobar enable Foobar])

if test "$PHP _foobar"! = "no"; Then
Php_new_extension (Foobar, foo.c bar.c, $ext _shared)
Fi

Php_arg_enable will automatically set the correct variables to ensure that the extension can be started by php_new_extension in shared mode.

The first argument to Php_new_extension is the name of the extension, and the second parameter is the resource file. The third parameter $ext _shared is set by Php_arg_enable/with for Php_new_extension.

Always use php_arg_enable or Php_arg_with to set up. Even if you are not going to publish your PHP module, these settings will ensure that your module and the PHP main module interface remain one.

Note: php_arg_enable and php_arg_with should be used to define whether the module is dynamically or statically compiled into PHP, just like the--enable-xxx and--with-xxx used when compiling PHP.

Create a resource file

Ext_skel can create some common code for your PHP module, you can also write some basic function definitions and C code to handle the parameters of the function. Specific information can be viewed readne. Ext_skel.

Don't worry about no examples, there are many modules in PHP for your reference, choose a simple point to start with and add your own code.

Note: Ext_skel can generate the required resource files and configuration files for the basic module and do not need to create them yourself.

modifying custom modules

Put the Config.m4 file and the resource file in the same directory, and then execute phpize (PHP 4.0 builds PHP with Phpize installed).

If your phpize is not in the system environment variable, you need to specify an absolute path, for example:

$/php/bin/phpize

This command automatically copies the necessary build files to the current directory and creates a configuration file based on CONFIG.M4.

With the above steps, you already have an independent extension.

Install extensions

Extensions can be installed by compiling the following command:

$./configure
[--with-php-config=/path/to/php-config]
$ make Install

Add shared support to a module

Sometimes the standalone extension needs to be shared for other modules to load. Next I will explain how to add shared support to the Foo module that has been created.

In the Config.m4 file, use Php_arg_with/php_arg_enable to set the extension so that you can automatically use--with-foo=shared[,..] or--enable-foo=shared[,..] Such an instruction is used as a compilation parameter.

In the Config.m4 file, the extension can be built using Php_new_extension (foo,.., $ext _shared).

Add the following code to your C language resource file:

#ifdef Compile_dl_foo
Zend_get_module (foo)
#endif

This section of the above mentioned above, here is just another emphasis.

PECL Website conventions

If you plan to publish your extension to PECL's website, you need to consider the following points:

Add LICENSE or COPYING to Package.xml

You need to define the version information in the extension header file, which will be used by the Foo_module_entry to declare the extended version:

#define Php_foo_version "1.2.3"

How to write a standalone PHP extension

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.