php擴充之關於hello world的詳細介紹

來源:互聯網
上載者:User


前言

這是一篇拖了很久就想寫的備忘錄,編寫php擴充一百度都是文章,但是很多文章是很老的了。有的例子都跑不通。有點尷尬。
此文用於記錄自己的筆記,當作備忘錄。

本文

1. 下載php安裝包

下載地址:php下載快鏈
本文選取的是php-5.6.7安裝包。
之後安裝php。

2. 建立擴充骨架

//跑到ext目錄cd php-5.6.7/ext///執行一鍵產生骨架的操作./ext_skel --extname=helloworld

如果看到以下提示說明建立成果

cd helloworldls

一下你會發現有如下檔案:

config.m4  config.w32  CREDITS  EXPERIMENTAL  helloworld.c  helloworld.php  php_helloworld.h  tests

3. 修改擴充的設定檔config.m4

去掉下面代碼之前的dnl 。(dnl相當於php的//)

##動態編譯選項,通過.so的方式連結,去掉dnl注釋PHP_ARG_WITH(helloworld, for helloworld support,Make sure that the comment is aligned:[  --with-helloworld             Include helloworld support])##靜態編譯選項,通過enable來啟用,去掉dnl注釋PHP_ARG_ENABLE(helloworld, whether to enable helloworld support,Make sure that the comment is aligned:[  --enable-helloworld           Enable helloworld support])

一般二者選一即可(看個人喜好吧,本文教程必須去掉enable的注釋)。

4. 進行編譯測試

phpize./configure --enable-helloworldmakemake install

然後到php.ini添加一下擴充

vim /usr/local/php/etc/php.ini// 添加擴充extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"extension = "helloworld.so"// 重啟php-fpm/etc/init.d/php-fpm restart

回到寫擴充的檔案夾,執行測試命令

php -d enable_dl=On myfile.php

看到如下字樣說明離勝利不遠了:

confirm_helloworld_compiledCongratulations! You have successfully modified ext/helloworld/config.m4. Module helloworld is now compiled into PHP.

confirm_helloworld_compiled是ext_skel自動產生的測試函數。

ps:如果本地安裝了兩個php版本,並且是用php7編寫擴充的話,可能會遇到以下問題:

/mydata/src/php-7.0.0/ext/helloworld/helloworld.c: 在函數‘zif_confirm_helloworld_compiled’中:/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: 錯誤:‘zend_string’未聲明(在此函數內第一次使用)/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: 錯誤:(即使在一個函數內多次出現,每個未聲明的標識符在其/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: 錯誤:所在的函數內也只報告一次。)/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: 錯誤:‘strg’未聲明(在此函數內第一次使用)

原因:編譯的環境不是php7。
解決辦法:php5 裡面沒有zend_string類型,用 char 替換,或者,修改你的php版本環境到php7

5. 建立helloworld函數

編輯helloworld.c,補充要實現的函數

##zend_function_entry helloworld_functions 補充要實現的函數const zend_function_entry helloworld_functions[] = {     PHP_FE(confirm_helloworld_compiled, NULL)       /* For testing, remove later. */    PHP_FE(helloworld,  NULL)       /* 這是補充的一行,尾巴沒有逗號 */    PHP_FE_END  /* Must be the last line in helloworld_functions[] */};

找到”PHP_FUNCTION(confirm_helloworld_compiled)”,另起一個函數編寫函數實體:

PHP_FUNCTION(helloworld) {    php_printf("Hello World!\n");    RETURN_TRUE;}

再走一遍編譯:

./configure --enable-helloworld && make && make install

測試一下是不是真的成功了:

php -d enable_dl=On -r "dl('helloworld.so');helloworld();"//輸出Hello World!

成功!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.