Zephir--最簡單的php擴充開發工具

來源:互聯網
上載者:User

更多關於PHP的技術文章http://www.codefrom.com/

php的擴充是用c語言編寫,Zend是語言引擎,PHP核心。在實際編寫php擴充的時候,需要使用大量的Zend Api,雖然運行效率高,但是其實難度也比較大。見:http://php.net/manual/zh/internals2.ze1.zendapi.php

Zephir提供了一種類似php的進階語言文法的方式,來自動產生擴充的c語言代碼,使編寫php擴充變得非常的簡單。

官網:http://www.zephir-lang.com/
官方的安裝教程:http://www.zephir-lang.com/welcome.html
ubuntu下需要安裝相關的依賴

$ sudo apt-get update$ sudo apt-get install git gcc make re2c php5 php5-json php5-dev libpcre3-dev

當然別的平台根據實際提示,安裝對應的程式即可

$ git clone https://github.com/phalcon/zephir$ cd zephir$ ./install-json$ ./install -c

測試安裝是否成功

$ zephir help

使用樣本如下:

$ zephir init utils

會在目前的目錄下產生

ext/ utils/ config.json

然後編輯utils子目錄下的greeting.zep 內容如下:

 namespace Utils; class Greeting {     public static function say()     {         var a = "hello world";         echo strtoupper(a);     } }

這裡的namespace是必須添加的,輸出大寫的"hello,world"。可以直接使用php的內建函數。
然後運行

$ zephir build

然後就會在擴充目錄下產生utils.so,修改php.ini添加添加該擴充。調用方法:

  

擴充閱讀:
zephir產生的c語言檔案預設在/ext/utils/greeting.zep.c,核心內容如下:

PHP_METHOD(Utils_Greeting, say) {    zval *a, *_0;    ZEPHIR_MM_GROW();    ZEPHIR_INIT_VAR(a);    ZVAL_STRING(a, "hello world", 1);    ZEPHIR_INIT_VAR(_0);    zephir_fast_strtoupper(_0, a);    zend_print_zval(_0, 0);    ZEPHIR_MM_RESTORE();}

可以發現zephir一方面直接zend api 如:zend_print_zval。另一方面封裝了zend api,如:zephir_fast_strtoupper 。直接修改該檔案的c代碼,使用原php擴充的編輯方法也是可以滴。

  • 聯繫我們

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