Table of Contents
PHP内核的学习--创建PHP扩展
Home php教程 php手册 PHP内核的学习--创建PHP扩展

PHP内核的学习--创建PHP扩展

Jun 13, 2016 am 09:07 AM
Kernel

PHP内核的学习--创建PHP扩展

PHP取得成功的一个主要原因之一是它拥有大量的可用扩展。web开发者无论有何种需求,这种需求最有可能在PHP发行包里找到。PHP发行包包括支持各种数据库,图形文件格式,压缩,XML技术扩展在内的许多扩展。

 

扩展API的引入使PHP3取得了巨大的进展,扩展API机制使PHP开发社区很容易的开发出几十种扩展。现在,两个版本过去了,API仍然和PHP3时的非常相似。扩展主要的思想是:尽可能的从扩展编写者那里隐藏PHP的内部机制和脚本引擎本身,仅仅需要开发者熟悉API。

 

有两个理由需要自己编写PHP扩展。第一个理由是:PHP需要支持一项她还未支持的技术。这通常包括包裹一些现成的C函数库,以便提供PHP接口。例如,如果一个叫FooBase的数据库已推出市场,你需要建立一个PHP扩展帮助你从PHP里调用FooBase的C函数库。这个工作可能仅由一个人完成,然后被整个PHP社区共享(如果你愿意的话)。第二个不是很普遍的理由是:你需要从性能或功能的原因考虑来编写一些商业逻辑。

 

假设你正在开发一个网站,需要一个把字符串重复n次的函数。下面是用PHP写的例子:

 

 

function util_str_repeat($string, $n){

    $result = "";

    for($i = 0; $i

        $result .= $string;

    }

    return $result;

}

 

util_str_repeat("One", 3);// returns "OneOneOne".

util_str_repeat("One", 1);// returns "One".

 

假设由于一些奇怪的原因,你需要时常调用这个函数,而且还要传给函数很长的字符串和大值n。这意味着在脚本里有相当巨大的字符串连接量和内存重新分配过程,以至显著地降低脚本执行速度。如果有一个函数能够更快地分配大量且足够的内存来存放结果字符串,然后把$string重复n次,就不需要在每次循环迭代中分配内存。

 

为扩展建立函数的第一步是写一个函数定义文件,该函数定义文件定义了扩展对外提供的函数原形。该例中,定义函数只有一行函数原形util_str_repeat() :

 

string util_str_repeat(string str, int n)

函数定义文件的一般格式是一个函数一行。你可以定义可选参数和使用大量的PHP类型,包括: bool, float, int, array等。

 

保存为util.def文件至PHP原代码目录树下(即与ext_skel文件放在同一目录下,我的目录是/usr/share/php5/)。

 

然后就是通过扩展骨架(skeleton)构造器运行函数定义文件的时机了。该构造器脚本就是ext_skel。假设你把函数定义保存在一个叫做util.def的文件里,而且你希望把扩展取名为util,运行下面的命令来建立扩展骨架:

 

sudo ./ext_skel --extname=util --proto=util.def

执行之后,我这里报了如下错误:

 

 

./ext_skel: 1: cd: can't cd to /usr/lib/php5/skeleton

Creating directory util

awk: cannot open /create_stubs (No such file or directory)

Creating basic files: config.m4 config.w32 .svnignore util.c./ext_skel: 216: ./ext_skel: cannot open /skeleton.c: No such file

 php_util.h./ext_skel: 234: ./ext_skel: cannot open /php_skeleton.h: No such file

 CREDITS./ext_skel: 238: ./ext_skel: cannot open /CREDITS: No such file

 EXPERIMENTAL./ext_skel: 242: ./ext_skel: cannot open /EXPERIMENTAL: No such file

 tests/001.phpt./ext_skel: 247: ./ext_skel: cannot open /tests/001.phpt: No such file

 util.php./ext_skel: 251: ./ext_skel: cannot open /skeleton.php: No such file

rm: cannot remove ‘function_entries’: No such file or directory

rm: cannot remove ‘function_declarations’: No such file or directory

rm: cannot remove ‘function_stubs’: No such file or directory

 [done].

 

To use your new extension, you will have to execute the following steps:

 

1.  $ cd ..

2.  $ vi ext/util/config.m4

3.  $ ./buildconf

4.  $ ./configure --[with|enable]-util

5.  $ make

6.  $ ./php -f ext/util/util.php

7.  $ vi ext/util/util.c

8.  $ make

 

Repeat steps 3-6 until you are satisfied with ext/util/config.m4 and

step 6 confirms that your module is compiled into PHP. Then, start writing

code and repeat the last two steps as often as necessary.

 

很明显是/usr/lib/php5/skeleton路径的错误,编辑ext_skel文件,将/usr/lib/php5/skeleton修改为/usr/share/php5/skeleton,然后移除掉生成的util文件夹,再次执行之前的命令,成功后提示如下:

 

 

Creating directory util

Creating basic files: config.m4 config.w32 .svnignore util.c php_util.h CREDITS EXPERIMENTAL tests/001.phpt util.php [done].

 

To use your new extension, you will have to execute the following steps:

 

1.  $ cd ..

2.  $ vi ext/util/config.m4

3.  $ ./buildconf

4.  $ ./configure --[with|enable]-util

5.  $ make

6.  $ ./php -f ext/util/util.php

7.  $ vi ext/util/util.c

8.  $ make

 

Repeat steps 3-6 until you are satisfied with ext/util/config.m4 and

step 6 confirms that your module is compiled into PHP. Then, start writing

code and repeat the last two steps as often as necessary.

 

然后采用静态编译的方式编译扩展。为了使扩展能够被编译,需要修改扩展目录util/下的config.m4文件。扩展没有包裹任何外部的C库,你需要添加支持–enable-util配置开关到PHP编译系统里(–with-extension 开关用于那些需要用户指定相关C库路径的扩展)。找到如下内容:

 

dnl PHP_ARG_ENABLE(util, whether to enable util support,

dnl Make sure that the comment is aligned:

dnl [  --enable-util           Enable util support])

将前面的dnl 去掉,修改为如下结果:

 

PHP_ARG_ENABLE(util, whether to enable util support,

Make sure that the comment is aligned:

[  --enable-util           Enable util support])

然后修改util.c文件,找到如下代码:

 

 

PHP_FUNCTION(util_str_repeat)

{

    char *str = NULL;

    int argc = ZEND_NUM_ARGS();

    int str_len;

    long n;

 

    if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE) 

        return;

 

    php_error(E_WARNING, "util_str_repeat: not yet implemented");

}

 

将其修改为如下代码:

 

 

PHP_FUNCTION(util_str_repeat)

{

    char *str = NULL;

    int argc = ZEND_NUM_ARGS();

    int str_len;

    long n;

    char *result; /* Points to resulting string */

    char *ptr; /* Points at the next location we want to copy to */

    int result_length; /* Length of resulting string */

 

    if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)

        return;

 

    /* Calculate length of result */

    result_length = (str_len * n);

    /* Allocate memory for result */

    result = (char *) emalloc(result_length + 1);

    /* Point at the beginning of the result */

    ptr = result;

 

    while (n--) {

        /* Copy str to the result */

        memcpy(ptr, str, str_len);

        /* Increment ptr to point at the next position we want to write to */

        ptr += str_len;

    }

 

    /* Null terminate the result. Always null-terminate your strings

    even if they are binary strings */

    *ptr = '\0';

    /* Return result to the scripting engine without duplicating it*/

    RETURN_STRINGL(result, result_length, 0);

}

  里面的具体内容,就不在这里说了,之后会慢慢写到。

 

然后就是编译,安装。在util目录下,命令如下(命令可能都需要加sudo):

 

phpize

./configure

make

make test

make install

然后配置生成的扩展文件,在php5.5版本中,进入到/etc/php5/mods-available目录下,创建util.ini文件,写入如下内容:

 

extension=util.so

然后enable util扩展

 

sudo php5enmod util

最后,重启php-fpm

 

sudo service php5-fpm restart

创建一个php文件,测试一下,测试文件如下:

 

for ($i = 1; $i

    print util_str_repeat("CraryPrimitiveMan ", $i);

    print "\n";

}

?>

执行结果如下:

 

CraryPrimitiveMan 

CraryPrimitiveMan CraryPrimitiveMan 

CraryPrimitiveMan CraryPrimitiveMan CraryPrimitiveMan

这样我们就成功创建了一个包含简单的PHP函数的扩展。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install the Linux kernel on Ubuntu 22.04 Detailed tutorial! How to install the Linux kernel on Ubuntu 22.04 Detailed tutorial! Mar 01, 2024 pm 10:34 PM

To install the Linux kernel on Ubuntu22.04, you can follow the following steps: Update the system: First, make sure your Ubuntu system is the latest, execute the following command to update the system package: sudoaptupdatesudoaptupgrade Download the kernel file: Visit the official Linux kernel website () to download Required kernel version. Select a stable version and download the source code file (with .tar.gz or .tar.xz extension), for example: wget Unzip the file: Use the following command to unzip the downloaded kernel source code file: tar-xflinux-5.14.tar. xz install build dependencies: Install the tools and dependencies required to build the kernel. Execute

Modify Linux kernel startup sequence Modify Linux kernel startup sequence Feb 23, 2024 pm 10:22 PM

Modify the kernel startup sequence of Linux 1. Modify the kernel startup sequence of RHEL6/CentOS6. Check the /etc/grub.conf file to determine the system kernel situation. According to the document, there are two kernel versions in the system, namely 2.6.32-573.18.1.el6.x86_64 and 2.6.32-431.23.3.el6.x86_64. Kernel versions are listed from top to bottom. In the grub.conf file, you can decide which kernel version to use when the system starts by adjusting the default parameters. The default value is 0, which means the system will boot the latest kernel version. A value of 0 corresponds to the first content listed in the grub.conf file.

Is the Android system based on the Linux kernel? Is the Android system based on the Linux kernel? Mar 14, 2024 pm 03:12 PM

Is the Android system based on the Linux kernel? Android system, as one of the most widely used mobile operating systems in the world, has always been said to be developed based on the Linux kernel. However, what is the real situation? Let’s explore this issue. First, let's understand the Linux kernel. The Linux kernel, as an open source operating system kernel, was first released by Linus Torvalds in 1991. It provides a good foundation for many operating systems, including And

Linux kernel main function analysis and analysis Linux kernel main function analysis and analysis Mar 14, 2024 am 11:27 AM

Linux kernel main function analysis and analysis The Linux kernel is a large and complex system, in which the main function plays a vital role. It is the entry point of the entire system and is responsible for initializing various subsystems, drivers and kernel modules. Finally start the entire operating system. This article will analyze and analyze the main function of the Linux kernel, and demonstrate its key functions and execution flow through specific code examples. In the Linux kernel, the entry point of the main function is start_k in the init/main.c file.

Explore the programming languages ​​used under the hood of the Linux kernel Explore the programming languages ​​used under the hood of the Linux kernel Mar 20, 2024 am 08:06 AM

Title: Exploring the programming language used at the bottom of the Linux kernel. As an open source, stable and reliable operating system kernel, the Linux kernel has a wide range of applications in the computer field. To have an in-depth understanding of the Linux kernel, you have to involve the programming language used at the bottom. In fact, the Linux kernel is mainly written in C language, which is an efficient, flexible and easy-to-maintain programming language that is very suitable for operating system development. This article will explore the bottom of the Linux kernel from a detailed perspective

Detailed explanation of Linux kernel source code storage location Detailed explanation of Linux kernel source code storage location Mar 14, 2024 pm 06:12 PM

Detailed explanation of the storage location of Linux kernel source code. Linux kernel source code is the core part of the Linux operating system. It contains the implementation code for various functions of the operating system. To understand where the Linux kernel source code is stored, we first need to understand the organizational structure of the Linux kernel. Linux kernel source code is usually stored in the /usr/src/linux or /usr/src/linux- directory. In this directory, there are many

Ubuntu compilation and installation kernel tutorial. Ubuntu compilation and installation kernel tutorial. Feb 19, 2024 pm 02:54 PM

Compiling and installing the Ubuntu kernel requires certain professional skills and practical experience. Here are the general steps, but please proceed with caution as this process may carry certain risks. Before you begin, be sure to back up important data and systems. Get the source code: Visit the Ubuntu official website () or the kernel developer website () to download the latest kernel source code. Unzip the source code to a suitable directory, such as /usr/src. Install compilation dependencies: Install the dependencies required to build the kernel. Open a terminal and execute the following command: sudoapt-getinstallbuild-essentiallibncurses-devbisonflexlibssl-devlibelf-d

Is the performance of win10 improved compared to win7? Detailed introduction Is the performance of win10 improved compared to win7? Detailed introduction Dec 23, 2023 am 09:04 AM

The more popular system now is the win10 system. Of course, there are also users who are preparing to upgrade. What these users are most concerned about is whether the performance of win10 is improved compared to win7? In fact, overall there are still some improvements, and the compatibility is also good. Is the performance of win10 improved compared to win7? Answer: The performance of win10 is improved compared to win7. The overall improvement is not very big, because the performance is mainly linked to the hardware. However, the win10 system has undergone a lot of optimizations so it can provide better assistance. Moreover, Microsoft no longer supports win7 updates, so win10 will be the most common system in the future. Comparative features of win10 compared to win7: 1. Configuration: win7 has been launched for more than ten years and has gone through a lot.

See all articles