2019-04-21 21:33:20 +08:00
|
|
|
# How to create a self-contained PHP extension
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
A self-contained extension can be distributed independently of the PHP source.
|
|
|
|
To create such an extension, two things are required:
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
* Configuration file (config.m4)
|
|
|
|
* Source code for your module
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
We will describe now how to create these and how to put things together.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## Prepairing your system
|
2000-07-31 21:44:06 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
While the result will run on any system, a developer's setup needs these tools:
|
2000-07-31 21:44:06 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
* GNU autoconf
|
|
|
|
* GNU libtool
|
|
|
|
* GNU m4
|
2000-07-31 21:44:06 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
All of these are available from
|
2000-07-31 21:44:06 +08:00
|
|
|
|
|
|
|
ftp://ftp.gnu.org/pub/gnu/
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## Converting an existing extension
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
Just to show you how easy it is to create a self-contained extension, we will
|
|
|
|
convert an embedded extension into a self-contained one. Install PHP and execute
|
|
|
|
the following commands.
|
2018-10-13 20:17:28 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```bash
|
|
|
|
mkdir /tmp/newext
|
|
|
|
cd /tmp/newext
|
|
|
|
```
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
You now have an empty directory. We will copy the files from the mysqli
|
|
|
|
extension:
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```bash
|
|
|
|
cp -rp php-src/ext/mysqli/* .
|
|
|
|
```
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
It is time to finish the module. Run:
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```bash
|
|
|
|
phpize
|
|
|
|
```
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
You can now ship the contents of the directory - the extension can live
|
|
|
|
completely on its own.
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
The user instructions boil down to
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```bash
|
|
|
|
./configure \
|
|
|
|
[--with-php-config=/path/to/php-config] \
|
|
|
|
[--with-mysqli=MYSQL-DIR]
|
|
|
|
make install
|
|
|
|
```
|
2000-05-03 05:51:48 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
The MySQL module will either use the embedded MySQL client library or the MySQL
|
|
|
|
installation in MYSQL-DIR.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## Defining the new extension
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
Our demo extension is called "foobar".
|
2018-10-13 20:17:28 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
It consists of two source files `foo.c` and `bar.c` (and any arbitrary amount of
|
|
|
|
header files, but that is not important here).
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
The demo extension does not reference any external libraries (that is important,
|
|
|
|
because the user does not need to specify anything).
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
`LTLIBRARY_SOURCES` specifies the names of the sources files. You can name an
|
|
|
|
arbitrary number of source files here.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## Creating the M4 configuration file
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
The m4 configuration can perform additional checks. For a self-contained
|
|
|
|
extension, you do not need more than a few macro calls.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```m4
|
2019-03-03 23:44:16 +08:00
|
|
|
PHP_ARG_ENABLE([foobar],
|
|
|
|
[whether to enable foobar],
|
|
|
|
[AS_HELP_STRING([--enable-foobar],
|
|
|
|
[Enable foobar])])
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2000-06-08 08:34:04 +08:00
|
|
|
if test "$PHP_FOOBAR" != "no"; then
|
2002-03-07 22:20:02 +08:00
|
|
|
PHP_NEW_EXTENSION(foobar, foo.c bar.c, $ext_shared)
|
2000-06-08 08:34:04 +08:00
|
|
|
fi
|
2019-04-21 10:53:02 +08:00
|
|
|
```
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
`PHP_ARG_ENABLE` will automatically set the correct variables, so that the
|
|
|
|
extension will be enabled by `PHP_NEW_EXTENSION` in shared mode.
|
2018-10-13 20:17:28 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
The first argument of `PHP_NEW_EXTENSION` describes the name of the extension.
|
|
|
|
The second names the source-code files. The third passes `$ext_shared` which is
|
|
|
|
set by `PHP_ARG_ENABLE/WITH` to `PHP_NEW_EXTENSION`.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
Please use always `PHP_ARG_ENABLE` or `PHP_ARG_WITH`. Even if you do not plan to
|
|
|
|
distribute your module with PHP, these facilities allow you to integrate your
|
|
|
|
module easily into the main PHP module framework.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## Create source files
|
2000-06-10 16:59:43 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
`ext_skel.php` can be of great help when creating the common code for all
|
|
|
|
modules in PHP for you and also writing basic function definitions and C code
|
|
|
|
for handling arguments passed to your functions. See `./ext/ext_skel.php --help`
|
|
|
|
for further information.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
As for the rest, you are currently alone here. There are a lot of existing
|
|
|
|
modules, use a simple module as a starting point and add your own code.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## Creating the self-contained extension
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
Put `config.m4` and the source files into one directory. Then, run `phpize`
|
|
|
|
(this is installed during `make install` by PHP).
|
2002-10-24 05:35:17 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
For example, if you configured PHP with `--prefix=/php`, you would run
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```bash
|
|
|
|
/php/bin/phpize
|
|
|
|
```
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
This will automatically copy the necessary build files and create configure from
|
|
|
|
your `config.m4`.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
And that's it. You now have a self-contained extension.
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## Installing a self-contained extension
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
An extension can be installed by running:
|
2000-05-03 04:59:46 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```bash
|
|
|
|
./configure \
|
|
|
|
[--with-php-config=/path/to/php-config]
|
|
|
|
make install
|
|
|
|
```
|
2000-05-23 06:33:51 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## Adding shared module support to a module
|
2000-05-23 06:33:51 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
In order to be useful, a self-contained extension must be loadable as a shared
|
|
|
|
module. The following will explain now how you can add shared module support to
|
|
|
|
an existing module called `foo`.
|
2000-05-23 06:33:51 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
1. In `config.m4`, use `PHP_ARG_WITH/PHP_ARG_ENABLE`. Then you will
|
|
|
|
automatically be able to use `--with-foo=shared[,..]` or
|
|
|
|
`--enable-foo=shared[,..]`.
|
2000-05-23 06:33:51 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
2. In `config.m4`, use `PHP_NEW_EXTENSION(foo,.., $ext_shared)` to enable
|
|
|
|
building the extension.
|
2000-05-23 06:33:51 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
3. Add the following lines to your C source file:
|
2000-05-23 06:33:51 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```c
|
|
|
|
#ifdef COMPILE_DL_FOO
|
|
|
|
ZEND_GET_MODULE(foo)
|
|
|
|
#endif
|
2019-04-21 10:53:02 +08:00
|
|
|
```
|
2013-10-14 22:07:26 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
## PECL site conformity
|
2013-10-14 22:07:26 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
If you plan to release an extension to the PECL website, there are several
|
|
|
|
points to be regarded.
|
2013-10-14 22:07:26 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
1. Add `LICENSE` or `COPYING` to the `package.xml`
|
2013-10-14 22:07:26 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
2. The following should be defined in one of the extension header files
|
2013-10-14 22:07:26 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
```c
|
|
|
|
#define PHP_FOO_VERSION "1.2.3"
|
|
|
|
```
|
2013-10-14 22:07:26 +08:00
|
|
|
|
2019-04-21 21:33:20 +08:00
|
|
|
This macros has to be used within your foo_module_entry to indicate the
|
|
|
|
extension version.
|