Mac安装体验Zephir

安装

系统要求

  • gcc >= 4.x/clang >= 3.x
  • re2c 0.13 or later
  • Zephir Parser >= 1.1.0
  • gnu make 3.81 or later
  • autoconf 2.31 or later
  • automake 1.14 or later
  • libpcre3
  • php development headers and tools

特别要注意的一点,对PHP的要求>5.5

使用brew安装 re2cjson-c

brew install re2c json-c

安装zephir

注意因为Zephir命令后续依赖源码包里的文件,意味着不能删除,所以把源码包放在安全的目录下。

mkdie -p ~/.tools
cd ~/.tools
git clone https://github.com/phalcon/zephir
cd zephir
./install -c

安装完之后执行zephir help,会看到以下内容

 _____              __    _
/__  /  ___  ____  / /_  (_)____
  / /  / _ \/ __ \/ __ \/ / ___/
 / /__/  __/ /_/ / / / / / /
/____/\___/ .___/_/ /_/_/_/
         /_/

Zephir version 0.10.7-8059e66568

Usage:
        command [options]

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

Options:
        --help|-h           Displays command help and exit
        -f([a-z0-9\-]+)     Enables compiler optimizations
        -fno-([a-z0-9\-]+)  Disables compiler optimizations
        -w([a-z0-9\-]+)     Turns a warning on
        -W([a-z0-9\-]+)     Turns a warning off

安装zephir-parser扩展

因为Zephir编译的扩展要依赖自身的解析器,所以得在运行的PHP版本编译该扩展。

git clone https://github.com/phalcon/php-zephir-parser
cd php-zephir-parser
./install

配置php.ini

[Zephir Parser]
extension=zephir_parser.so

检查是否安装成功

php -m|grep "Zephir Parser"

编写一个简单的扩展

初始化项目

mkdir -p ~/phpexts
cd ~/phpexts
zephir init greeting
cd greeting

使用ls命令能看到以config.json,ext,greeting ,分别介绍一下

config.json 设定档,里面应该会写着 namepsace 是什么

ext/ 最后生成的 PHP Extension 会在这个资料夹找到

greeting/ 放置 .zep 档案的目录(类似 PSR-0 的 Class 目录规则,不过都是小写)

编写.zep代码

cd greeting
vim greeting.zep

源码如下

namespace Greeting;

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

编译

zephir build

看到以下提示表示编译成功

Preparing for PHP compilation...
Preparing configuration file...
Compiling...
Installing...
Password:
Extension installed!
Add extension=greeting.so to your php.ini
Don't forget to restart your web server

配置php.ini

[Greeting]
extension=greeting.so

检查是否安装成功

php -m|grep -i greeting

测试代码

php -r "\Greeting\Greeting::say();"

输出结果为

Hello world!
打赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!