PHP: A case for Zephir

Sean Wragg
Jan 6, 2016 · 2 min read

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.

Image for post
(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 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
  • 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

Findings

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

The below graphic is of two XHProf snapshots taken from a Zend Framework 1 (ZF1) application. The first is with our custom Zephir Zend\Registry; the second is of the existing stock PHP ZF1 Zend_Registry.

Image for post

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


Originally published at seanwragg.com on January 6, 2016.

sean3z

Code, Comics, and Fhqwhgads!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store