Bil*_*win 25
用C/C++编写的软件肯定比PHP中的代码运行得更快.您可以在C/C++中编写扩展并将其链接到PHP.PHP手册在此处介绍了这一点:http: //php.net/manual/en/internals2.php
其他答案提供了编写PHP扩展的其他教程的链接,您可以谷歌搜索"PHP扩展教程"以查找更多内容.
但是,这是否是你的应用程序中正确的做法是另一个故事.大多数专家都认为PHP运行得很好,足够快,98%的应用程序.PHP不够快的实例通常不是由于语言,而是程序员创建的低效应用程序体系结构.这是一个无法通过在C/C++中重写部分应用程序来弥补的弱点.
Jen*_*och 24
我知道你可以通过制作PHP文件和使用技术上实现PHP扩展
require_once.
此功能的基础是include语句,其中包括并评估指定的文件.扩展不是正确的术语,因为您只是包含另一个PHP脚本文件.PHP扩展以编译模块的形式为语言提供附加功能.
但如果你用C或C++编写扩展,它会优化性能吗?
是的,它优化了性能.这就是编写CPhalcon或YAF等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: