PHP扩展开发-01:第一个扩展
為什么80%的碼農都做不了架構師?>>> ??
<HEAD>
???下面的操作是在Ubuntu 12.04下,并且已經搭建了LAMP環境.
</HEAD>
一.下載PHP源碼
1.首先安裝GIT????? ? ? ? ??
2.克隆PHP源碼
cd / git clone https://github.com/php/php-src.git ls 會看到php-src文件夾
3.進入ext目錄
cd php-src/ext ls 會看見很多擴展如curl,pdo等,同時還會看見用來建立擴展的腳本ext_skel
二.建立骨架修改參數
1.利用ext_skel建立骨架
./ext_skel --extname=yourname
yourname為你想建立的擴展的名字,我們先建一個,例如為rube
建立好后當前文件夾下會出現rube這個文件夾
cd rube 2.修改config,m4的參數
vim config.m4dnl Otherwise use enable:PHP_ARG_ENABLE(rube, whether to enable rube support, dnl Make sure that the comment is aligned:[? --enable-rube?????????? Enable rube support])
將PHP_ARG_ENABLE(rube, whether to enable rube support 和 [? --enable-rube???????????? Enable rube support]? 這兩行前面的dnl 去掉 。修改成如上所示
三.編寫php_rube.h 和 rube.c
1.編輯php_rube.h
vim php_rube.h
? 在php_rube.h的最后面添加
PHP_FUNCTION(confirm_rube_compiled); PHP_FUNCTION(hello); hello 為你要創建的那個函數
2.編輯rube.c
vim rube.cconst zend_function_entry rube_functions[] = {PHP_FE(confirm_rube_compiled,?? ?NULL)?? ?? PHP_FE(hello,??? NULL)???????? PHP_FE_END? }; ? 修改zend_function_entry rube_functions[] , 在PHP_FE(confirm_rube_compiled,?? NULL)后面添加
?PHP_FE(hello,??? NULL)
3.編寫函數
接下來編寫hello這個函數,首先編寫一個簡單的輸出"Hello my first extention"的函數。。。
?? 在rube.c的最后面添加
PHP_FUNCTION(hello) { char *arg = "Hello my first extention!"; int len; char *strg;len = spprintf(&strg, 0, "%s\n", arg); RETURN_STRINGL(strg, len, 0); } 保存后退出
四.編譯代碼
1.編譯成so文件
cd /php-src/ext/rube whereis phpize 看是否存在phpize
如果存在運行phpize,否則用
sudo apt-get install php5-dev 進行安裝后運行 phpize
然后
./configure --with-php-config=你的php-config位置 如果找不到php-config的位置
whereis php-config
./configure --with-php-config=你的php-config位置 接著
make 在編譯過程中如果你的代碼出現錯誤,會報錯。
make這步中如果提示
Build complete
說明編譯成功.然后
安裝好后rube.so文件會在當前文件夾下的modules文件夾下,同時也會被安裝在系統提示的位置(也就是你的系統中php擴展的默認安裝位置),我的提示如下:
Installing shared extensions: /usr/lib/php5/20090626+lfs/
說明rube.so被安裝在/usr/lib/php5/20090626+lfs/目錄下
ls /usr/lib/php5/20090626+lfs/ #查看是否在此文件夾下
2.修改php.ini
找到php.ini文件 然后打開在文件最后添加
extension=/usr/lib/php5/20090626+lfs/rube.so #我的擴展在/usr/lib/php5/20090626+lfs/rube.so 你可以相應修改
重啟apache
五.進行測試
在你網站根目錄創建test.php
vim test.php<?phpecho hello();
結果為
Hello my first extention
轉載于:https://my.oschina.net/rube/blog/158910
總結
以上是生活随笔為你收集整理的PHP扩展开发-01:第一个扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: getAttribute与getPara
- 下一篇: 【SVM】A Practical Gui