# PHP: A case for Zephir

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

![](https://cdn.hashnode.com/uploads/posts/6aefcf97a529a56101dcc7c8/83d5d43c-2a3b-4ba0-b096-86bce29e0b38.jpg)

(Ze)ndEngine / (P)HP / (I)nte(r)mediate

Zephir is a language that addresses the major needs of a PHP developer trying to write and compile code that can be executed by PHP. For all intents and purposes, it’s a transpiler that allows you to write code in a custom “Zephir” language that compiles down to C++.

### Installation

The [installation docs](http://docs.zephir-lang.com/en/latest/install.html) will suggest using Composer however, I’ve found it easier to compile from source.



```
$ brew install re2c
$ git clone git@github.com:phalcon/zephir.git
$ cd zephir && ./install -c
$ zephir help
```



### Some basics

There are some basic details to remember when writing Zephir code:

- No opening tag/line (`<?php`, `<?`, etc.)
- Defining a property value will make it immutable, define initial values in the `__construct` method if you expect values to change
- Methods support parameter as well as return type-hinting
- Using `string`, `array`, `int` type hints for parameters makes them immutable, use `var` if the values will change
- [There is a Sublime Text syntax highlighter available](https://github.com/phalcon/zephir-sublime)
- String values are defined using “double-quotes”, not single-quotes
- Defining/updating variables is done using the `let` keyword (`let variable = "value"`)
- You cannot (currently) create classes in the [Global Space](http://php.net/manual/en/language.namespaces.global.php)

### Findings

In this case, a custom version of the `library/Zend/Registry.php` file was deployed.

The below graphic is of two [XHProf](https://pecl.php.net/package/xhprof) snapshots taken from a [Zend Framework 1](https://framework.zend.com/manual/1.11/en/introduction.overview.html) (ZF1) application. The first is with our custom Zephir `Zend\Registry`; the second is of the existing stock PHP ZF1 `Zend_Registry`.

![](https://cdn.hashnode.com/uploads/posts/6aefcf97a529a56101dcc7c8/e2aaed1e-84f2-45e6-a5f9-82e598f94b38.png)

Perhaps the most important stat to observe is `Incl. Wall Time (microsec)`. At ~7k calls, the stock `Zend_Registry` takes ~197 milliseconds. Our custom `Zend\Registry` reduced the PHP time spent in retrieving registry items from to 95ms. As expected, the CPU load is also reduced by half.

Our Zephir equivalent executes **53% faster** than the stock `Zend_Registry`.

### Resources

- [https://github.com/phalcon/zephir](https://github.com/phalcon/zephir)
- [http://zephir-lang.com/](http://zephir-lang.com/)
- [https://github.com/sean3z/zendframework-zephir](https://github.com/sean3z/zendframework-zephir)
- [http://docs.zephir-lang.com/en/latest/install.html](http://docs.zephir-lang.com/en/latest/install.html)
- [http://www.sitepoint.com/zephir-build-php-extensions-without-knowing-c/](http://www.sitepoint.com/zephir-build-php-extensions-without-knowing-c/)

*Originally published at* [*seanwragg.com*](http://seanwragg.com/2016/01/06/php-a-case-for-zephir/) *on January 6, 2016.*
