Zephir 简介

什么是 Zephir
Zephir 是一种中间语言,以接近 PHP 的语法来编写代码,然后转换成 PHP 扩展。
安装
要构建PHP扩展并使用Zephir,您需要满足以下要求 (以centos7 + php7.2 为例)
编译环境
官方示例以ubuntu,需要安装如下扩展
sudo apt-get install git gcc make re2c php php-json php-dev libpcre3-dev build-essential
centos yum安装
yum install -y git gcc make re2c autoconf automake libtool pcre pcre-devel

yum groupinstall -y “Development Tools”

某些库可能某些源没有,可以更换源或者下载源码安装
php 扩展
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
下载最新版本,我们使用phar包
cd /usr/local/bin

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

chmod 777 zephir.phar

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

检查是否安装成功
zephir help
安装成装显示如下图

创建扩展
初始化一个应用
zephir init first
生成如下两个目录和一个文件

编写代码
注意:在 Zephir中, 每个文件都必须包含一个类 (并且只有一个类)。 每个类都必须有一个命名空间, 并且目录结构必须与所使用的类和命名空间的名称相匹配。
使用 phpstorm 作为IDE,安装 Zephir 插件。
firstfirsthello.zep
namespace First;

class Hello
{

public function world() {
echo “Hello world!”;
}

public function zephir() {
echo “Hello Zephir!”;
}

}
编译
zephir build
第一次执行,运气不好的话会失败。详细日志见 compile-errors.log 文件的内容。
如果build成功,会自动生成 first.so 文件到 extension 目录 ,你需要编辑 php.ini 填加扩展

测试一下
<?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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理