1. 云栖社区>
  2. PHP教程>
  3. 正文

PHP-CPP 编写php扩展

作者:用户 来源:互联网 时间:2017-11-30 20:41:15

php扩展cpp编写

PHP-CPP 编写php扩展 - 摘要: 本文讲的是PHP-CPP 编写php扩展, 环境: ubuntu16 server php7.0 一 安装php-cpp git clone https://github.com/CopernicaMarketingSoftware/PHP-CPP.gitcd PHP-CPP/ma

环境:


ubuntu16 server


php7.0


一 安装php-cpp


git clone https://github.com/CopernicaMarketingSoftware/PHP-CPP.git
cd PHP-CPP/
make
sudo make install

二编写一个扩展


a.下载个空扩展包


wget http://www.php-cpp.com/EmptyExtension.tar.gz
tar -zxvf EmptyExtension.tar.gz
cd EmptyExtension/

b.修改文件


vim Makefile
#修改扩展名和配置文件地址就行
#我改成这样:
# NAME =my_extension
# INI_DIR=/etc/php/7.0/mods-available/
#修改配置文件
mv yourextension.ini my_extension.ini
vim my_extension.ini
#修改为:
#extension=my_extension.so
#修改主文件
vim main.cpp
#修改为:
#include
#include
void myFunction()
{
Php::out << "example output" << std::endl;
}
/**
*tell the compiler that the get_module is a pure C function
*/
extern "C" {
/**
*Function that is called by PHP right after the PHP process
*has started, and that returns an address of an internal PHP
*strucure with all the details and features of your extension
*
*@return void* a pointer to an address that is understood by PHP
*/
PHPCPP_EXPORT void *get_module()
{
// static(!) Php::Extension object that should stay in memory
// for the entire duration of the process (that's why it's static)
static Php::Extension extension("my_extension", "1.0");
// @todoadd your own functions, classes, namespaces to the extension
extension.add("myFunction");
// return the extension
return extension;
}
}

c.编译,安装


make
sudo make install

d.修改PHP配置文件并重启服务


sudo vim /etc/php/7.0/cli/php.ini
#加入extension = my_function.so
sudo service php7.0-fpm restart

三 测试扩展


vim test.php<?php
myFunction();
php test.php
#显示:example output

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php , 扩展 , cpp 编写 ,以便于您获取更多的相关知识。