更多关于PHP的技术文章http://www.codefrom.com/php
php的扩展是用c语言编写,Zend是语言引擎,PHP内核。在实际编写php扩展的时候,须要使用大量的Zend Api,虽然运行效率高,可是其实难度也比较大。见:http://php.net/manual/zh/internals2.ze1.zendapi.phphtml
Zephir提供了一种相似php的高级语言语法的方式,来自动生成扩展的c语言代码,使编写php扩展变得很是的简单。git
官网:http://www.zephir-lang.com/
官方的安装教程:http://www.zephir-lang.com/welcome.html
ubuntu下须要安装相关的依赖github
$ sudo apt-get update $ sudo apt-get install git gcc make re2c php5 php5-json php5-dev libpcre3-dev
固然别的平台根据实际提示,安装对应的程序便可json
$ git clone https://github.com/phalcon/zephir $ cd zephir $ ./install-json $ ./install -c
测试安装是否成功ubuntu
$ zephir help
使用示例以下:api
$ zephir init utils
会在当前目录下生成函数
ext/ utils/ config.json
而后编辑utils子目录下的greeting.zep 内容以下:测试
namespace Utils; class Greeting { public static function say() { var a = "hello world"; echo strtoupper(a); } }
这里的namespace是必须添加的,输出大写的"hello,world"。能够直接使用php的内置函数。
而后运行ui
$ zephir build
而后就会在扩展目录下生成utils.so,修改php.ini添加添加该扩展。调用方法:
<?php echo Utils\Greeting::say(), "\n";
扩展阅读:
zephir生成的c语言文件默认在/ext/utils/greeting.zep.c,核心内容以下:
PHP_METHOD(Utils_Greeting, say) { zval *a, *_0; ZEPHIR_MM_GROW(); ZEPHIR_INIT_VAR(a); ZVAL_STRING(a, "hello world", 1); ZEPHIR_INIT_VAR(_0); zephir_fast_strtoupper(_0, a); zend_print_zval(_0, 0); ZEPHIR_MM_RESTORE(); }
能够发现zephir一方面直接zend api 如:zend_print_zval。另外一方面封装了zend api,如:zephir_fast_strtoupper 。直接修改该文件的c代码,使用原php扩展的编辑方法也是能够滴。