【PHP 擴展開發】Zephir 簡介

什麼是 Zephirphp

Zephir 是一種中間語言,以接近 PHP 的語法來編寫代碼,而後轉換編譯成 PHP 擴展,旨在簡化 PHP 擴展的建立和可維護性。利用編譯來提升性能和資源消耗,又不須要關注內存管理等複雜操做。git

安裝github

要使用 Zephir 開發 PHP 擴展 ,須要知足如下要求 (以 centos7 + php7.2 爲例)json

編譯環境ubuntu

官方示例以 ubuntu ,須要安裝以下擴展centos

sudo apt-get install git gcc make re2c php php-json php-dev libpcre3-dev build-essential

咱們用 centos ,yum 安裝api

yum install -y git gcc make re2c autoconf automake libtool pcre pcre-devel

yum groupinstall -y "Development Tools"

某些庫可能某些源沒有,能夠更換源或者下載源碼安裝php7

php 擴展phpstorm

Zephir parser >= 1.1.0性能

wget https://github.com/phalcon/php-zephir-parser/archive/v1.2.0.tar.gz

mv v1.2.0.tar.gz php-zephir-parser-v1.2.0.tar.gz

tar -xvzf php-zephir-parser-v1.2.0.tar.gz

cd php-zephir-parser-1.2.0/

/usr/local/php7/bin/phpize

./configure --with-php-config=/usr/local/php7/bin/php-config

make && make install

gmp (PHP 源碼中包含該擴展,默認狀況下不安裝)

擴展安裝方法大同小異,不另說明

以源碼編譯的方式追加安裝這兩擴展,並配置ini文件

extension=gmp.so
extension=zephir_parser.so

安裝 Zephir

Zephir 目前有兩個大版本在維護 0.10.X和0.11.X。兩個版本差異比較大,咱們選擇0.11.X,下載最新版本,使用 phar 包

cd /usr/local/bin

wget https://github.com/phalcon/zephir/releases/download/0.11.10/zephir.phar

chmod 755 zephir.phar

ln -s /usr/local/bin/zephir.phar zephir

檢查是否安裝成功

zephir help

安裝成裝顯示以下圖

clipboard.png

建立擴展

初始化一個應用

zephir init first

生成以下兩個目錄和一個文件

clipboard.png

編寫代碼

注意:在 Zephir 中, 每一個文件都必須包含一個類 (而且只有一個類)。 每一個類都必須有一個命名空間, 而且目錄結構必須與所使用的類和命名空間的名稱相匹配。

使用 phpstorm 做爲 IDE,安裝 Zephir 插件。

first\first\hello.zep

namespace First;

class Hello
{

    public static function world() {
        echo "Hello world!";
    }

    public static function zephir() {
        echo "Hello Zephir!";
    }

}

編譯

zephir build

第一次執行,運氣很差的話會失敗。詳細日誌見 compile-errors.log 文件的內容。

若是 build 成功,會自動生成 first.so 文件到 extension 目錄 ,你須要編輯 php.ini 填加擴展

clipboard.png

測試一下

<?php

echo First\Hello::world(), "\n";

就這樣,你也會 PHP 擴展開發了

附1:Available commands:

init                Initializes a Zephir extension
    builddev            Generates/Builds/Installs a Zephir extension in development mode
    api                 Generates a HTML API based on the classes exposed in the extension
    clean               Cleans any object files created by the extension
    generate            Generates C code from the Zephir code without compiling it
    build               Generates/Builds/Installs a Zephir extension
    stubs               Generates stubs that can be used in a PHP IDE
    help                Displays this help and exit
    fullclean           Cleans any object files created by the extension (including files generated by phpize)
    compile             Compile a Zephir extension
    install             Installs the extension in the extension directory (may require root password)
    version             Shows the Zephir version

附2:Windows 編譯擴展的方法
https://blog.csdn.net/abc1035...

相關文章
相關標籤/搜索