如何制作PHP扩展

Mar*_*lor 23 php c c++

我知道你可以通过制作PHP文件和使用技术上实现PHP扩展require_once.

但如果你用C或C++编写扩展,它会优化性能.

如果是这样,你会如何为此做一个"你好世界"?

Bil*_*win 25

用C/C++编写的软件肯定比PHP中的代码运行得更快.您可以在C/C++中编写扩展并将其链接到PHP.PHP手册在此处介绍了这一点:http: //php.net/manual/en/internals2.php

其他答案提供了编写PHP扩展的其他教程的链接,您可以谷歌搜索"PHP扩展教程"以查找更多内容.

但是,这是否是你的应用程序中正确的做法是另一个故事.大多数专家都认为PHP运行得很好,足够快,98%的应用程序.PHP不够快的实例通常不是由于语言,而是程序员创建的低效应用程序体系结构.这是一个无法通过在C/C++中重写部分应用程序来弥补的弱点.

  • 是的,毫无疑问,11年后事情发生了变化。因此,请使用我的第二个建议:谷歌搜索。 (3认同)
  • 谷歌搜索只会导致人们问 SO T_T 问题 (2认同)

Jen*_*och 24

我知道你可以通过制作PHP文件和使用技术上实现PHP扩展require_once.

此功能的基础是include语句,其中包括并评估指定的文件.扩展不是正确的术语,因为您只是包含另一个PHP脚本文件.PHP扩展以编译模块的形式为语言提供附加功能.

但如果你用C或C++编写扩展,它会优化性能吗?

是的,它优化了性能.这就是编写CPhalconYAF等PHP扩展的原因.

如何制作"Hello World"PHP扩展?

我将描述如何通过五个步骤构建"Hello World"PHP扩展.

基于Debian的操作系统是必需的,因为我们需要使用apt-get获取一些工具和依赖项.

第1步 - 设置构建环境/要求

PHP扩展是编译的C代码.我们需要一个shell(应该已经安装),一个编辑器(您的选择),一个编译器(这里我们将使用GCC),PHP本身和PHP开发依赖项进行构建.

sudo apt-get install build-essential php7.0 php7.0-dev
Run Code Online (Sandbox Code Playgroud)

第2步 - 配置

我们需要在基本配置文件中描述我们的扩展和形成它的文件:

文件: config.m4

PHP_ARG_ENABLE(php_helloworld, Whether to enable the HelloWorldPHP extension, [ --enable-helloworld-php Enable HelloWorldPHP])

if test "$PHP_HELLOWORLD" != "no"; then
    PHP_NEW_EXTENSION(php_helloworld, php_helloworld.c, $ext_shared)
fi
Run Code Online (Sandbox Code Playgroud)

如您所见,NEW_EXTENSION包含一个C文件:php_helloworld.c.

第3步 - 代码

让我们为我们的扩展创建C代码.

首先,我们创建一个头文件:

php_helloworld.h

// we define Module constants
#define PHP_HELLOWORLD_EXTNAME "php_helloworld"
#define PHP_HELLOWORLD_VERSION "0.0.1"

// then we declare the function to be exported
PHP_FUNCTION(helloworld_php);
Run Code Online (Sandbox Code Playgroud)

其次,我们创建源文件:

php_helloworld.c

// include the PHP API itself
#include <php.h>
// then include the header of your extension
#include "php_helloworld.h"

// register our function to the PHP API 
// so that PHP knows, which functions are in this module
zend_function_entry helloworld_php_functions[] = {
    PHP_FE(helloworld_php, NULL)
    {NULL, NULL, NULL}
};

// some pieces of information about our module
zend_module_entry helloworld_php_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_HELLOWORLD_EXTNAME,
    helloworld_php_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    PHP_HELLOWORLD_VERSION,
    STANDARD_MODULE_PROPERTIES
};

// use a macro to output additional C code, to make ext dynamically loadable
ZEND_GET_MODULE(helloworld_php)

// Finally, we implement our "Hello World" function
// this function will be made available to PHP
// and prints to PHP stdout using printf
PHP_FUNCTION(helloworld_php) {
    php_printf("Hello World! (from our extension)\n");
}
Run Code Online (Sandbox Code Playgroud)

第4步 - 构建

现在,我们已准备好构建扩展.

首先,我们为PHP扩展准备构建环境:

phpize
Run Code Online (Sandbox Code Playgroud)

然后我们配置构建并启用扩展:

./configure --enable-php-helloworld
Run Code Online (Sandbox Code Playgroud)

最后,我们可以构建它:

make
sudo make install
Run Code Online (Sandbox Code Playgroud)

第5步 - 测试

要测试我们的PHP扩展,让我们加载helloworld_php.so扩展文件并执行我们的函数helloworld_php():

php -d extension=php_helloworld.so -r 'helloworld_php();'

完成:)


Building on Windows

If you try to build an Windows (YIKES!), then you need to adjust the steps a bit:

Step 2 - Config

File: config.w32

ARG_ENABLE("helloworld", "helloworld support", "yes");

if (PHP_HELLOWORLD == "yes") {
    EXTENSION("helloworld", "php_helloworld.c");
}
Run Code Online (Sandbox Code Playgroud)

Step 4 - Build

Use nmake instead of make.


List of helpful resources:


Pab*_*ruz 6

是关于PHP扩展的教程.它是否会优化性能,取决于您尝试包含在扩展上的内容.但我不会写一个PHP扩展只是为了优化.如果我别无选择,我会写一个.IE包装一个通用的C库,使其直接在PHP中可用...

  • 链接断开。 (2认同)
  • 这是一个存档版本:https://web.archive.org/web/20111101050807/http://devzone.zend.com/article/1021 (2认同)