zephir 编译php,PHP扩展开发之Zephir

如果你时常关注PHP框架Phalcon,那么应该知道Phalcon的团队为了更好的开发Phalcon,开发了一门高级语言——Zephir。

Zephir,一种开源的高级语言,旨在简化PHP扩展的创建和可维护性,重点关注类型和内存安全性。

如果你没有C/C++语言的基础,又需要以PHP扩展的方式开发业务,那么可以选择Zephir。

使用Zephir开发扩展的环境很容易搭建,按照官方文档步骤,可以很快的把环境准备好。而且最新版本的Zephir已经提供.phar文件,下载这个文件添加进path环境变量就能开始开发了。

[root@ef40a04d7af1 ~]# zephir

_____ __ _

/__ / ___ ____ / /_ (_)____

/ / / _ \/ __ \/ __ \/ / ___/

/ /__/ __/ /_/ / / / / / /

/____/\___/ .___/_/ /_/_/_/

/_/

Zephir 0.11.11 by Andres Gutierrez and Serghei Iakovlev (b661a58)

Usage:

command [options] [arguments]

Options:

-h, --help Display this help message

-q, --quiet Do not output any message

-V, --version Display this application version

--ansi Force ANSI output

--no-ansi Disable ANSI output

-n, --no-interaction Do not ask any interactive question

--dumpversion Print the Zephir version — and don't do anything else

-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:

api Generates a HTML API based on the classes exposed in the extension

build Generates/Compiles/Installs a Zephir extension

clean Cleans any object files created by the extension

compile Compile 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

help Displays help for a command

init Initializes a Zephir extension

install Installs the extension in the extension directory (may require root password)

list Lists commands

stubs Generates stubs that can be used in a PHP IDE

下面使用Zephir开发一个简单PHP扩展—Hutils—来介绍基本开发流程。

1. 初始化扩展目录

zephir init Hutils

cd hutils && tree -L 2

//会生成如下目录结构

.

|-- config.json

|-- ext

| `-- kernel

`-- hutils

2. 编写扩展代码

进入hutils/hutils/目录,创建一个common.zep文件。

namespace Hutils;

class Common

{

public static function say()

{

echo "hello world!";

}

}

3. 编译扩展

zephir build

Preparing for PHP compilation...

Preparing configuration file...

Compiling...

Installing...

Extension installed.

Add "extension=hutils.so" to your php.ini

! [NOTE] Don't forget to restart your web server

然后在php.ini中添加extension=hutils.so后,就可以在php代码中调用hutils扩展中的方法了。

[root@ef40a04d7af1 ~]# php -r "echo Hutils\Common::say();"

Hello World!

扩展开发完成后,也可以将hutils\ext打包发布,这样就可以脱离Zephir开发环境,使用一般扩展编译安装步骤使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值