Heim  >  Artikel  >  Backend-Entwicklung  >  PHP Extension开发(Zephir版本)

PHP Extension开发(Zephir版本)

WBOY
WBOYOriginal
2016-06-23 13:39:59841Durchsuche

上篇介绍了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来调用:
<?phpecho Utils\Greeting::say(), "\n";
打印hello world!  成功了!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn