PHP Extension開發(Zephir版本)

來源:互聯網
上載者:User
上篇介紹了C語言開發PHP擴充的方法, 現在介紹使用Zephir開發擴充的方法.

關於Zephir需要簡單介紹一下: Zephir 是為PHP開發人員提供的能夠編寫可編譯/靜態類型的進階語言.是優秀的Phalcon團隊為開發第二版本架構所編寫的新興語言,其文法優雅,開發擴充非常方便,執行效率上據說是與C不分上下, 他的編譯流程如下:Zephir -> C -> bin.

Zephir的目的是建立物件導向庫或者架構, 所以它與Clang擴充不同, 不能編寫非OOP的模組.

想瞭解更可以請訪問官網: http://zephir-lang.com/


Zephir的安裝

安裝zephir的前提條件:
建立一個PHP擴充和使用Zephir需要以下要求:
gcc >= 4.x/clang >= 3.x
re2c 0.13 or later
gnu make 3.81 or later
autoconf 2.31 or later
automake 1.14 or later
libpcre3
php development headers and tools


如果在使用ubuntu,可以這樣安裝包:
$ sudo apt-get update$ sudo apt-get install git gcc make re2c php5 php5-json php5-dev libpcre3-dev
同時,確保你也安裝了PHP開發庫(PHP安裝時):
$ phpize -vConfiguring for:PHP Api Version:         20121113Zend Module Api No:      20121212
目前Zephir編譯器必須從Github複製:
$ git clone https://github.com/phalcon/zephir
運行Zephir安裝程式(編譯/建立解析器):
$ cd zephir$ ./install-json$ ./install -c
MAC下安裝前可能還需要安裝automake:
$ brew install automake

檢測安裝, 檢查是否可以從任何目錄Zephir通過執行:

$ zephir help _____              __    _/__  /  ___  ____  / /_  (_)____  / /  / _ \/ __ \/ __ \/ / ___/ / /__/  __/ /_/ / / / / / //____/\___/ .___/_/ /_/_/_/         /_/Zephir version 0.5.9a...


使用Zephir開發擴充

我們要做的第一件事就是產生一個擴充架構,這將提供給我們擴充的基本結構
下面這個例子我們建立了一個名為"utils"的擴充:

$ zephir init utils
之後,目前的目錄下就有一個"utils"目錄了:
utils/
ext/
utils/
ext/ 目錄下包含的是編譯器產生擴充所需的代碼(C源碼).
utils/ 目錄下是我們的Zephir源碼.

我們需要切到工作目錄下,也就是utils下,開始編寫代碼:
$ cd utils$ lsext/ utils/ config.json
其中的config.json,這個檔案包含配置,可以用來改變Zephir的行為.
添加我們的第一個類:
// utils/utils/greeting.zepnamespace Utils;class Greeting{    public static function say()    {        echo "hello world!";    }}
現在,需要告訴Zephir把我們的項目編譯成擴充,這一步要在項目目錄下執行:
$ zephir build
如果順利會列印以下資訊:
...
Extension installed!
Add extension=utils.so to your php.ini
Don't forget to restart your web server

初始測試:
$ php -m
[PHP Modules]
....
SPL
standard
tokenizer
utils
....

好的,我們用PHP來調用:
列印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.