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
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

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

Some basics

  • 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

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
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!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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