资讯专栏INFORMATION COLUMN

PHPCPP安装以及hello world

zhangqh / 367人阅读

摘要:最主要的是大大的降低了扩展的开发难度。以前开发扩展,初始化项目对于小白来说都可能是个灾难,而对于希望开发扩展的可以说是一个福音。在不接触底层的情况下,采用是一个很好的选择方案。以后会每周至少更新两篇关于的文章记录并分享自己的使用的开发经验。

学习了一段时间做PHP扩展开发,由于C的难度较大,内存回收,指针每一个都可以能让初学者望而却步,加上C开发效率太低,小型企业对于这种高大上的开发,还是少触碰为好。但是有时候PHP开发执行效率确实太低,而且存在很大的资源浪费,并且这个又是无法避免的(比如excel),混合开发让项目的迁移和可维护性都降低了。

一次偶然机会发现了PHPCPP,C++开发PHP的扩展,C++不用说了吧!执行效率最接近C的语言。最主要的是PHPCPP大大的降低了PHP扩展的开发难度。以前开发扩展,初始化项目对于小白来说都可能是个灾难,而PHPCPP对于希望开发扩展的PHPER可以说是一个福音。

在不接触PHP底层的情况下,采用PHPCPP是一个很好的选择方案。下面废话不多说,上干货。

首先选择一个你常用的linux版本,然后下载PHPCPP

 git clone https://github.com/CopernicaMarketingSoftware/PHP-CPP.git

然后编译并安装PHPCPP

make &&sudo  make install

这样PHPCPP就安装成功了。

从PHPCPP下载一个空项目http://www.php-cpp.com/EmptyE...

这里说明一下,默认最新PHPCPP是只支持PHP7的,如果需要支持PHP5.X需要下载他的另外一个版本,API一样,只是sdk不同,所以采用PHPCPP开发的扩展源码是相通的,只是需要在对应版本编译。

言归正传,空项目中的Makefile,可以修改NAME,这个是你的扩展文件名称,其他都保持不变

#
#   Makefile template
#
#   This is an example Makefile that can be used by anyone who is building
#   his or her own PHP extensions using the PHP-CPP library. 
#
#   In the top part of this file we have included variables that can be
#   altered to fit your configuration, near the bottom the instructions and
#   dependencies for the compiler are defined. The deeper you get into this
#   file, the less likely it is that you will have to change anything in it.
#

#
#   Name of your extension
#
#   This is the name of your extension. Based on this extension name, the
#   name of the library file (name.so) and the name of the config file (name.ini)
#   are automatically generated
#

NAME                =   yourextension

#
#   Php.ini directories
#
#   In the past, PHP used a single php.ini configuration file. Today, most
#   PHP installations use a conf.d directory that holds a set of config files,
#   one for each extension. Use this variable to specify this directory.
#
#   In Ubuntu 14.04 Apache 2.4 is used, which uses the mods-available directory
#   instead of a conf.d directory. In 16.04 the directory changed yet again.
#   This has to be checked.
#

UBUNTU_MAJOR  := $(shell /usr/bin/lsb_release -r -s | cut -f1 -d.)
OVER_SIXTEEN  := $(shell echo "${UBUNTU_MAJOR} >= 16" | bc)
OVER_FOURTEEN := $(shell echo "${UBUNTU_MAJOR} >= 14" | bc)

ifeq (${OVER_SIXTEEN}, 1)
    INI_DIR     =   /etc/php/7.0/mods-available/
else ifeq (${OVER_FOURTEEN}, 1)
    INI_DIR     =   /etc/php5/mods-available/
else
    INI_DIR     =   /etc/php5/conf.d/
endif

#
#   The extension dirs
#
#   This is normally a directory like /usr/lib/php5/20121221 (based on the 
#   PHP version that you use. We make use of the command line "php-config" 
#   instruction to find out what the extension directory is, you can override
#   this with a different fixed directory
#

EXTENSION_DIR       =   $(shell php-config --extension-dir)

#
#   The name of the extension and the name of the .ini file
#
#   These two variables are based on the name of the extension. We simply add
#   a certain extension to them (.so or .ini)
#

EXTENSION           =   ${NAME}.so
INI                 =   ${NAME}.ini

#
#   Compiler
#
#   By default, the GNU C++ compiler is used. If you want to use a different
#   compiler, you can change that here. You can change this for both the 
#   compiler (the program that turns the c++ files into object files) and for
#   the linker (the program that links all object files into the single .so
#   library file. By default, g++ (the GNU C++ compiler) is used for both.
#

COMPILER            =   g++
LINKER              =   g++

#
#   Compiler and linker flags
#
#   This variable holds the flags that are passed to the compiler. By default, 
#   we include the -O2 flag. This flag tells the compiler to optimize the code, 
#   but it makes debugging more difficult. So if you"re debugging your application, 
#   you probably want to remove this -O2 flag. At the same time, you can then 
#   add the -g flag to instruct the compiler to include debug information in
#   the library (but this will make the final libphpcpp.so file much bigger, so
#   you want to leave that flag out on production servers).
#
#   If your extension depends on other libraries (and it does at least depend on
#   one: the PHP-CPP library), you should update the LINKER_DEPENDENCIES variable
#   with a list of all flags that should be passed to the linker.
#

COMPILER_FLAGS      =   -Wall -c -O2 -std=c++11 -fpic -o
LINKER_FLAGS        =   -shared
LINKER_DEPENDENCIES =   -lphpcpp

#
#   Command to remove files, copy files and create directories.
#
#   I"ve never encountered a *nix environment in which these commands do not work. 
#   So you can probably leave this as it is
#

RM                  =   rm -f
CP                  =   cp -f
MKDIR               =   mkdir -p

#
#   All source files are simply all *.cpp files found in the current directory
#
#   A built-in Makefile macro is used to scan the current directory and find 
#   all source files. The object files are all compiled versions of the source
#   file, with the .cpp extension being replaced by .o.
#

SOURCES             =   $(wildcard *.cpp)
OBJECTS             =   $(SOURCES:%.cpp=%.o)

#
#   From here the build instructions start
#

all:                    ${OBJECTS} ${EXTENSION}

${EXTENSION}:           ${OBJECTS}
                        ${LINKER} ${LINKER_FLAGS} -o $@ ${OBJECTS} ${LINKER_DEPENDENCIES}

${OBJECTS}:
                        ${COMPILER} ${COMPILER_FLAGS} $@ ${@:%.o=%.cpp}

install:        
                        ${CP} ${EXTENSION} ${EXTENSION_DIR}
                        ${CP} ${INI} ${INI_DIR}

clean:
                        ${RM} ${EXTENSION} ${OBJECTS}
main.c源码

#include 
#include 

void myFunction()
{
    Php::out << "hello world" << std::endl;
}

extern "C" {
    PHPCPP_EXPORT void *get_module() {
        static Php::Extension extension("my_extension", "1.0");
        extension.add("myFunction");
        return extension;
    }
}

这样一个扩展代码就完成了,执行编译

make
然后将生成的xxx.so拷贝到PHP扩展目录,并在PHP.INI加入
extension=yourextension.so

执行php -m查看是否加载了扩展,如果已经成功加载那么,就会显示my_extension在里面了

php  -m

那么PHP中如何调用呢?如果没有使用命名空间那么可以这样


如果使用了需要加上


这样第一个扩展就完成了。

以后会每周至少更新两篇关于PHPCPP的文章记录并分享自己的使用PHPCPP的开发经验。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22254.html

相关文章

  • PHP扩展开发教程2 - 编写第一个扩展 hello world

    摘要:二下载第一个扩展第一个扩展的源码已经在上准备好了,直接用命令克隆,或者手工下载都可以。第四步确认扩展已经安装成功使用命令可以查看目前已经安装的所有扩展。 PHP扩展是高级PHP程序员必须了解的技能之一,对于一个初入门的PHP扩展开发者,怎么才能开发一个成熟的扩展,进入PHP开发的高级领域呢?本系列开发教程将手把手带您从入门进入高级阶段。本教程系列在linux下面开发(推荐使用cento...

    Berwin 评论0 收藏0
  • PHP扩展开发教程3 - 开发一个我们自己的数学函数库

    摘要:下载命令行浏览器下载网址和仓库网址一样一不带参数,没有返回值的扩展函数写法函数功能打印以内的素数函数名称如何注册扩展函数必须在函数体中,注册函数,以便能在中能直接调用。函数有返回值,返回值类型设置为。 PHP扩展是高级PHP程序员必须了解的技能之一,对于一个初入门的PHP扩展开发者,怎么才能开发一个成熟的扩展,进入PHP开发的高级领域呢?本系列开发教程将手把手带您从入门进入高级阶段。本...

    Barry_Ng 评论0 收藏0
  • PHP扩展开发教程1 - 相关开发技术对比及介绍

    摘要:四使用语言开发是我重点推荐的扩展开发框架,简明易懂,功能强大,开发效率高,代码易维护,执行速度快。优点三支持,的扩展开发有两套扩展开发框架,分别支持,,虽然框架代码有两个,但是接口却是一样的。 PHP扩展是高级PHP程序员必须了解的技能之一,对于一个初入门的PHP扩展开发者,怎么才能开发一个成熟的扩展,进入PHP开发的高级领域呢?本系列开发教程将手把手带您从入门进入高级阶段。本教程系列...

    alaege 评论0 收藏0
  • 360正式开源zendAPI 项目,让 PHP 的扩展开发成为一种享受

    摘要:从而让的扩展开发成为一种享受,不用在考虑不同版本带来的差异性,让开发者专注于自身的业务逻辑。怎么参与交流下面是我们项目的线上交流群和微信的二维码,大家可以扫码加入技术圈欢迎大家在这两个平台上与我们进行互动特别感谢无线电安全研究部独角兽团队 360开源项目介绍: 360开源官方github: https://github.com/qihoo360 今天给大家介绍一个360最新开源的产...

    DrizzleX 评论0 收藏0
  • zendAPI 项目简介

    摘要:项目是什么是对的接口使用的最新标准进行而面向对象的封装,从而屏蔽了底层的接口复杂性,加快开发扩展的效率。国内同类型的项目推荐目前国内有一个跟比较类似的项目,这个项目是项目作者开发,值得推荐。项目名字项目的地址是大家有兴趣可以研究。 项目Logo showImg(https://segmentfault.com/img/bVVtW8?w=716&h=218); zendAPI 是什么? ...

    Pluser 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<