Zephir是高性能PHP框架Phalcon厂商开发的开源高级/特定领域语言,它在降低PHP扩展开发、维护的复杂性的同时,也致力于保证类型和内存安全。Zephir是Zend Engine/PHP/Intermediate缩写,读作“zaefire”。

Zephir的主要特点:

Type system dynamic/static
Memory safety pointers or direct memory management aren’t allowed
Compilation model ahead of time
Memory model task-local garbage collection

下面通过实例讲解如何在CentOS 7上使用Zephir开发一个PHP扩展。

环境需求:
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

1.软件安装

sudo yum -y install php72-php php72-php-fpm php72-php-devel php72-zephir gcc re2c

2.确认zephir安装成功

zephir version

3.生成utils扩展骨架,并查看扩展的目录结构(其中config.json是配置文件,ext用于存放生成的PHP扩展,utils目录用于存放Zephir代码)

zephir init utils
cd utils
ls -al utils
-rw-rw-r--. 1 allen allen 2087 Nov 15 06:09 config.json
drwxr-xr-x. 9 allen allen 4096 Nov 15 05:49 ext
drwxrwxr-x. 2 allen allen 44 Nov 15 04:02 utils

4.config.json配置文件如下,可根据需要进行修改。

{
    "stubs": {
        "path": "ide\/%version%\/%namespace%\/",
        "stubs-run-after-generate": false
    },
    "api": {
        "path": "doc\/%version%",
        "theme": {
            "name": "zephir",
            "options": {
                "github": null,
                "analytics": null,
                "main_color": "#3E6496",
                "link_color": "#3E6496",
                "link_hover_color": "#5F9AE7"
            }
        }
    },
    "warnings": {
        "unused-variable": true,
        "unused-variable-external": false,
        "possible-wrong-parameter": true,
        "possible-wrong-parameter-undefined": false,
        "nonexistent-function": true,
        "nonexistent-class": true,
        "non-valid-isset": true,
        "non-array-update": true,
        "non-valid-objectupdate": true,
        "non-valid-fetch": true,
        "invalid-array-index": true,
        "non-array-append": true,
        "invalid-return-type": true,
        "unreachable-code": true,
        "nonexistent-constant": true,
        "not-supported-magic-constant": true,
        "non-valid-decrement": true,
        "non-valid-increment": true,
        "non-valid-clone": true,
        "non-valid-new": true,
        "non-array-access": true,
        "invalid-reference": true,
        "invalid-typeof-comparison": true,
        "conditional-initialization": true
    },
    "optimizations": {
        "static-type-inference": true,
        "static-type-inference-second-pass": true,
        "local-context-pass": true,
        "constant-folding": true,
        "static-constant-class-folding": true,
        "call-gatherer-pass": true,
        "check-invalid-reads": false,
        "internal-call-transformation": false
    },
    "namespace": "utils",
    "name": "utils",
    "description": "An utils helper extension",
    "author": "aiddroid",
    "version": "0.0.1",
    "verbose": false,
    "requires": {
        "extensions": []
    },
    "globals": {
        "enable_cache": {
            "type": "bool",
            "default": true
        }
    }
}

5.新增Zephir扩展类~/utils/utils/helper.zep(每一个*.zep文件对应一个类)

namespace Utils;

class Helper
{

    public static function square(int number)
    {
        var square = 0;
        let square = number * number;
        return square;
    }
    
    public static function starts_with(string str, string start)
    {
        return strpos(str, start) === 0;
    }
}

6.编译扩展

zephir build
Compiling...
Installing...
Extension installed!
Add extension=utils.so to your php.ini
Don't forget to restart your web server

 

7.生成的扩展文件utils.so在~/utils/ext/modules/utils.so

8.移动utils.so到php modules目录(我的是/opt/remi/php72/root/usr/lib64/php/modules)

9.修改php.ini,启用utils扩展

extension=utils

10.测试utils扩展

php -r "echo Utils\Helper::square(2);"
4

11.查看phpinfo

注意事项:

  • zephir代码中,使用变量前必须声明,动态变量(可被修改、重新赋值的变量)使用var声明,静态变量使用int, string 等类型直接声明,两个例子:
    • var money = 0;let money = 1000;//动态变量,可使用let重新赋值
    • int height = 100;//静态变量,一旦声明,无法修改
  • 每个zep文件中有且仅能有一个class,每个class必须要有namespace声明,namespace路径必须与zep文件路径一致

Post Navigation