「Zephir」

an open source, high-level/domain specific language designed to ease the creation and maintainability of extensions for PHP with a focus on type and memory safety.

Docs: https://docs.zephir-lang.com/en/latest/index.html

Installation

https://docs.zephir-lang.com/en/latest/install.html

最好看以下 install 脚本,如果没有 sudo 命令,使用 install-nosudo,其实手动安装就行了;

这个安装还是比较简单的;

相关的IDE

Atom:

Emacs:

Tutorial Or Demo

Basic Syntax

基本语法

https://docs.zephir-lang.com/en/latest/language.html
In this chapter, we’ll discuss the organization of files and namespaces, variable declarations, miscellaneous syntax conventions, and a few other concepts.

every file must contain a class (and just one class).
Every class must have a namespace and the directory structure must match the names of classes and namespaces used.(namespce 与 文件夹名匹配,class 名与文件名匹配);

Zephir doesn’t support global variables, accessing global variables from the PHP userland is not allowed. However, you can access the PHP’s super-globals

Local Symbol Table

https://docs.zephir-lang.com/en/latest/language.html#local-symbol-table
这个要提别说明以下,关于符号表的问题:

Every method or context in PHP has a symbol table that allows to write variables in a very dynamic way:

<?php

$b = 100;
$a = "b";
echo $$a; // prints 100

Zephir does not implement this feature since all variables are compiled down to low-level variables and there is no way to know which variables do exist in a specific context.
If you want to create a variable in the current PHP symbol table, you can use the following syntax:

//Set variable $name in PHP
let {"name"} = "hello"; // 此时创建的 name 变量在当前符号表中,使用 $name 引用变量;

//Set variable $price in PHP
let name = "price";
let {name} = 10.2; // 此时,修改了符号表中的 $price 的值;

Types

https://docs.zephir-lang.com/en/latest/types.html
Zephir is both dynamic and static typed. In this chapter we highlight the supported types and its behavior:

Operators

https://docs.zephir-lang.com/en/latest/operators.html
Zephir’s set of operators are similar to the ones PHP and also inherits some of their behaviors.

Arrays

https://docs.zephir-lang.com/en/latest/arrays.html
Array manipulation in Zephir provides a way to use PHP arrays. An array is an implementation of a hash table.

Classes and Objects

https://docs.zephir-lang.com/en/latest/oop.html

Zephir promotes object-oriented programming, this is why you can only export methods and classes in extensions, also you will see that most of the time, runtime errors raise exceptions instead of fatal errors or warnings.

Built-In Methods

https://docs.zephir-lang.com/en/latest/builtin-methods.html
核心内容:As mentioned before, Zephir promotes object-oriented programming, variables related to static types can be also handled as objects.

Control Structures

https://docs.zephir-lang.com/en/latest/control.html

Exceptions

https://docs.zephir-lang.com/en/latest/exceptions.html

Calling Functions

https://docs.zephir-lang.com/en/latest/functions.html
PHP has a rich library of functions that you can use within your extensions.

Closures

https://docs.zephir-lang.com/en/latest/closures.html
You can use closures or anonymous functions in Zephir

Custom optimizers

https://docs.zephir-lang.com/en/latest/optimizers.html
Most common functions in Zephir use internal optimizers. An ‘optimizer’ works like an interceptor for function calls.
An ‘optimizer’ replaces the call for the function in the PHP userland by direct C-calls which are faster and have a lower overhead improving performance.

这部分的内容会复杂一些,但也不是很难懂;

Configuration File

https://docs.zephir-lang.com/en/latest/config.html
Every Zephir extension has a configuration file called config.json.
This file is read by Zephir every time you build or generate the extension and it allows the developer to modify the extension or compiler behavior.

Extension Globals

https://docs.zephir-lang.com/en/latest/globals.html
PHP extensions provide a way to define globals within an extension.
Reading/writing globals should be faster than any other global mechanisms (like static members).
You can use extension globals to set up configuration options that change the behavior of your library.

定义全局变量;

Extension globals cannot be dynamically accessed since the C-code generated by globals_get/globals_set optimizers must be resolved at compilation time

Phpinfo() sections

https://docs.zephir-lang.com/en/latest/phpinfo.html
Like most extensions, Zephir extensions are able to show information at the phpinfo() output.
These information is usually related to directives, enviroment data, etc.
By default, every Zephir extension automatically adds a basic table to the phpinfo() output showing the extension version.

Static Analysis

https://docs.zephir-lang.com/en/latest/sa.html
Zephir’s compiler provides static analysis of the compiled code.
The idea behind this feature is to help the developer to find potential problems and avoid unexpected behaviors.

编译器对代码的静态分析;

Optimizations

https://docs.zephir-lang.com/en/latest/optimizations.html
Because the code in Zephir is sometimes very high-level, a C compiler might not be in the ability to optimize this code enough.

Compiler Warnings

https://docs.zephir-lang.com/en/latest/warnings.html
The compiler raise warnings when it finds situations where the code can be improved or a potential error can be avoided.
Warnings can be enabled via command line parameters or can be added to the config.json to enable or disable them permanently