PHP擴充開發(一)DEMO

來源:互聯網
上載者:User
##入門    1.下載PHP源碼(我的版本是5.6.13)    2.常規編譯安裝  [可以看我以前的blog](http://my.oschina.net/lwl1989/blog/511295)    3.使用ext_skel    ```        cd phpsrc/ext        ./ext_skel --extname=name  // name 是指你要寫得擴充的名字,下文我們都是t2    ```出現提示```1.  $ cd ..2.  $ vi ext/t2/config.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-t25.  $ make6.  $ ./sapi/cli/php -f ext/t2/t2.php7.  $ vi ext/t2/t2.c8.  $ make```根據提示進行操作其中第二步    我們應該修改config.m4的10、11、12行    去除掉dnl[我的理解是ZEND一種注釋的方案]到這個時候,我們就可以進行編譯安裝了```/usr/local/php/bin/phpize   //執行phpize./configure --with-php-config=/usr/local/php/bin/php-configmake make install```在php.ini裡開啟 t2.so檔案  你的模組就載入好了查看php -m 可以發現t2模組已經被載入##編碼(HELLO  WORLD)但是目前而言,這個擴充是沒有任何功能的。於是我們需要加入新的功能。這時,我們可以對t2.c檔案進行編寫(當然你也可以寫在別的檔案裡,在t2.c裡面include)```PHP_FUNCTION(t2_hello){    printf("hello world\n");}```這樣重新編譯後,運行 php -r "t2_hello();"是不行的,為什麼呢?因為我們沒有將t2_hello載入到函數列表原型:```typedef struct _zend_function_entry {    char *fname;    void (*handler)(INTERNAL_FUNCTION_PARAMETERS);    unsigned char *func_arg_types;} zend_function_entry;```> 參數                描述fname              提供給PHP中調用的函數名。例如mysql_connecthandler            負責處理這個介面函數的指標。func_arg_types     標記參數。可以設定為NULL我們在產生的中加入一行```static const zend_function_entry t2_functions[] = {    //在這加入     {NULL, NULL, NULL}}```    PHP_FE(t2_hello,  NULL)> 注意,zend_function_entry的最後一項一定是{NULL, NULL, NULL},Zend引擎就是靠這個判斷匯出函數列表是否完畢的。然後重新編譯,運行php -r "t2_hello();"這時,我們的hello world!到此完成了時間不太多,下次我們再說關於變數和參數值傳遞的處理還有幾個模組的魔術方法(zend_module_entry)。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.