Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a separate PHP extension

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to write an independent PHP extension, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Independent PHP extensions can be distributed independently of the PHP source code. To create such an extension, you need to prepare two things:

Configuration file (config.m4)

Your module source code

Next, let's describe how to create these files and combine them.

Prepare the system tools

If you want the extension to compile and run successfully on the system, you need to be prepared to switch to the following tools:

GNU autoconf

GNU automake

GNU libtool

GNU m4

All of the above is available from ftp://ftp.gnu.org/pub/gnu/.

Note: these are all tools that can only be used in Unix-like environments.

Modify an existing extension

To show how easy it is to create a stand-alone extension, let's first change an extension that has been embedded in PHP to a stand-alone extension. Install PHP and execute the following command:

$mkdir / tmp/newext

$cd / tmp/newext

Now you have an empty directory. Let's copy the files in the mysql extension directory:

$cp-rp php-4.0.X/ext/mysql/*.

# Note: it seems that this README really needs to be updated

The mysql extension has been removed from # PHP7

The extension is done here, execute:

$phpize

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

The user needs to use the following command when compiling:

$. / 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 located in the MySQL directory.

Note: it means that to write PHP extensions, you need to have PHP installed and download a copy of the PHP source code.

Define a new extension

Let's name the example extension "foobar".

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

The sample extension does not reference any external libraries (this is important because the user does not need to specify some special 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 above are the configuration options in the Makefile.in file, you can refer to xdebug.

Modify the configuration file of the M4 suffix

The M4 configuration file can specify some additional checks. For a stand-alone extension, you only need to do 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 automatically sets the correct variables to ensure that the extension can be started by PHP_NEW_EXTENSION in shared mode.

The * * parameter of 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 don't plan to release your PHP module, these settings ensure that the interface between your module and the PHP main module remains the same.

Note: PHP_ARG_ENABLE and PHP_ARG_WITH should be used to define whether the module is dynamically extended 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 general code for your PHP module, or you can write some basic function definitions and C code to handle function parameters. For more information, please see READNE.EXT_SKEL.

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

Note: ext_skel can generate the resource files and configuration files needed by the basic module, and do not need to create their own.

Modify custom module

Put the config.m4 file and the resource file in the same directory, and then execute phpize (phpize is installed when PHP version 4.0 or later compiles PHP).

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 configuration files based on config.m4.

Through the above steps, you already have a separate extension.

Install extension

Extensions can be compiled and installed with the following command:

$. / configure

[--with-php-config=/path/to/php-config]

$make install

Add sharing support to the module

Sometimes stand-alone extensions need to be shared and loaded by other modules. Next I'll explain how to add sharing support to the foo module that has already 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 instructions are used as compilation parameters.

In the config.m4 file, use PHP_NEW_EXTENSION (foo,.., $ext_shared) so that the extension can be built.

Add the following code to your C language resource file:

# ifdef COMPILE_DL_FOO

ZEND_GET_MODULE (foo)

# endif

All the above has been mentioned in this paragraph, but it is just emphasized here.

PECL site convention

If you plan to launch your website that extends to PECL, you need to consider the following points:

Add LICENSE or COPYING to package.xml

The version information needs to be defined in the extension header file, and this macro will be called by foo_module_entry to declare the extended version:

# define PHP_FOO_VERSION "1.2.3"

The above is how to write a separate PHP extension, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report