代码之家  ›  专栏  ›  技术社区  ›  Mark Lalor

如何进行PHP扩展

  •  19
  • Mark Lalor  · 技术社区  · 14 年前

    我知道你可以通过创建一个PHP文件并使用 require_once .

    但是如果您在C或C++中写了一个扩展,它会优化性能吗?。

    如果是的话,你会怎么做一个“你好的世界”呢?

    4 回复  |  直到 6 年前
        1
  •  24
  •   Bill Karwin    14 年前

    用C/C++编写的软件肯定比PHP中的代码运行得快。你可以在C/C++中写一个扩展,并把它链接到PHP中。PHP手册包括以下内容: http://php.net/manual/en/internals2.php

    其他答案提供了指向其他编写PHP扩展的教程的链接,您可以通过谷歌搜索“PHP扩展教程”来了解更多信息。

    但在你的应用程序中,这是否是正确的做法是另一回事。大多数专家都认为PHP运行得很好,速度足以满足98%的应用程序。PHP不够快的实例通常不是由于语言的原因,而是程序员创建的一个效率低下的应用程序体系结构。这是一个弱点,不能通过改写C/C++中的应用程序来弥补。

        2
  •  21
  •   Jens A. Koch    6 年前

    我知道你可以通过创建一个PHP文件并使用 require_once .

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

    但是如果你在C或C++中写了一个扩展,它会优化性能吗?

    是的,它优化了性能。这就是为什么PHP扩展喜欢 CPhalcon YAF 写的。

    如何进行“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
    

    步骤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
    

    如您所见,新的扩展名包含一个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);
    

    其次,我们创建源文件:

    php?地狱世界.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");
    }
    

    步骤4-生成

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

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

    phpize
    

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

    ./configure --enable-php-helloworld
    

    最后,我们可以构建它:

    make
    sudo make install
    

    步骤5-测试

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

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

    完成:)


    在窗户上建造

    如果你试图建立一个窗口(Yikes!), 然后您需要稍微调整步骤:

    步骤2-配置

    文件: config.w32

    ARG_ENABLE("helloworld", "helloworld support", "yes");
    
    if (PHP_HELLOWORLD == "yes") {
        EXTENSION("helloworld", "php_helloworld.c");
    }
    

    步骤4-生成

    使用 nmake 而不是 make .


    有用资源列表:

        3
  •  6
  •   Pablo Santa Cruz    14 年前

    Here 是关于PHP扩展的教程。它是否会优化性能,这取决于您在扩展中要包装什么。但是我不会写一个PHP扩展 只是 用于优化目的。如果我没有选择的话,我会写一篇。也就是说,包装一个通用的C库,使它直接在PHP中可用…

        4
  •  0
  •   khalid J-A    6 年前

    我想 (但不确定) 你可以通过做一个 dll 文件并将其放入与 php 安装文件。如果我前面的话是正确的,你可以这样做。 (dll) file 在Visual Studio中