摘要:最主要的是大大的降低了擴展的開發難度。以前開發擴展,初始化項目對于小白來說都可能是個災難,而對于希望開發擴展的可以說是一個福音。在不接觸底層的情況下,采用是一個很好的選擇方案。以后會每周至少更新兩篇關于的文章記錄并分享自己的使用的開發經驗。
學習了一段時間做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的開發經驗。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/22254.html
摘要:二下載第一個擴展第一個擴展的源碼已經在上準備好了,直接用命令克隆,或者手工下載都可以。第四步確認擴展已經安裝成功使用命令可以查看目前已經安裝的所有擴展。 PHP擴展是高級PHP程序員必須了解的技能之一,對于一個初入門的PHP擴展開發者,怎么才能開發一個成熟的擴展,進入PHP開發的高級領域呢?本系列開發教程將手把手帶您從入門進入高級階段。本教程系列在linux下面開發(推薦使用cento...
摘要:下載命令行瀏覽器下載網址和倉庫網址一樣一不帶參數,沒有返回值的擴展函數寫法函數功能打印以內的素數函數名稱如何注冊擴展函數必須在函數體中,注冊函數,以便能在中能直接調用。函數有返回值,返回值類型設置為。 PHP擴展是高級PHP程序員必須了解的技能之一,對于一個初入門的PHP擴展開發者,怎么才能開發一個成熟的擴展,進入PHP開發的高級領域呢?本系列開發教程將手把手帶您從入門進入高級階段。本...
摘要:四使用語言開發是我重點推薦的擴展開發框架,簡明易懂,功能強大,開發效率高,代碼易維護,執行速度快。優點三支持,的擴展開發有兩套擴展開發框架,分別支持,,雖然框架代碼有兩個,但是接口卻是一樣的。 PHP擴展是高級PHP程序員必須了解的技能之一,對于一個初入門的PHP擴展開發者,怎么才能開發一個成熟的擴展,進入PHP開發的高級領域呢?本系列開發教程將手把手帶您從入門進入高級階段。本教程系列...
摘要:從而讓的擴展開發成為一種享受,不用在考慮不同版本帶來的差異性,讓開發者專注于自身的業務邏輯。怎么參與交流下面是我們項目的線上交流群和微信的二維碼,大家可以掃碼加入技術圈歡迎大家在這兩個平臺上與我們進行互動特別感謝無線電安全研究部獨角獸團隊 360開源項目介紹: 360開源官方github: https://github.com/qihoo360 今天給大家介紹一個360最新開源的產...
摘要:項目是什么是對的接口使用的最新標準進行而面向對象的封裝,從而屏蔽了底層的接口復雜性,加快開發擴展的效率。國內同類型的項目推薦目前國內有一個跟比較類似的項目,這個項目是項目作者開發,值得推薦。項目名字項目的地址是大家有興趣可以研究。 項目Logo showImg(https://segmentfault.com/img/bVVtW8?w=716&h=218); zendAPI 是什么? ...
閱讀 3095·2021-10-15 09:41
閱讀 3167·2021-09-22 16:05
閱讀 2405·2021-09-22 15:19
閱讀 2873·2021-09-02 15:11
閱讀 2446·2019-08-30 15:52
閱讀 832·2019-08-30 11:06
閱讀 1001·2019-08-29 16:44
閱讀 1240·2019-08-23 18:18