Installing Zephir with PHP 7.2

For any PHP developer wanting rapidly develop PHP extensions or optimize their PHP application, installing Zephir is a good first step towards eliminating their PHP application’s execution bottlenecks. While installation of Zephir is not terribly complicated, installing Zephir on Linux systems (such as Ubuntu) or on OS X is much easier when you understand Zephir’s architecture and how the compiler integrates with the PHP interpreter. There are two top-level steps in the installation procedure which must be done in the right order.

Zephir is a new high level language that makes writing PHP extensions much easier than before, allowing developers to create amazing performance enhancements through custom PHP extensions. Zephir is still a young project. However, normal project growth pains such as fragmented documentation and lack of distribution packages needn’t prevent use of the awesome new tool.

Zephir Installation Overview

There are two top level tasks when installing Zephir. They must be done in the following order.

  1. Install Zephir Parser first! – The Zephir parser is a PHP extension that needs to be installed on your development machines php-cli. You must install the extension before you can install the Zephir compiler. The Zephir compiler documentation does not say this explicitly enough. There is a dependency by the Zephir compiler that requires the Zephir Parser PHP extension to be installed and working for the compiler to install and run properly. Like any extension, it is basically a matter of dropping the into the right directory, and putting an `extension=zephir_parser` directive into the right .ini file. Now, here’s the tricky part.

    When you run the install script that compiles the Zephir Parser extension and should place the .so file in the right place, but you really benefit from running the installation script with the correct flags pointing to the target version of PHP that you’ll be using for development. So, run the installation script with the following flags adapted for your version of PHP and operating system:

    sudo ./install –phpize /usr/bin/phpize7.2 –php-config /usr/bin/php-config7.2

    That’s the trick to getting Zephir Parser to compile on Ubuntu 16.04 against PHP 7.2 as of February 2018. I suspect it may be blocking potential users on other distributions as well. It is buried in the “Advanced Compilation” section of the readme, but the flags for the install script currently have no documentation and the need in some instances to compile with the –phpize and –php-config flags needs to be more prominently stated.

  2. Install the Zephir compiler only after you’ve installed the PHP extension on the php-cli command line tool. Use `php -i | grep zephir` and look for evidence that the Zephir Parser extension is installed. Only then should you attempt to install the compiler.

    The compiler installation can be a little tricky too. If you want the compiler to install the binary in /usr/local/bin (and you probably do) you have to use the -c flag on the install script. I.e:

    sudo ./install -c

    Otherwise… I’m not clear what the intent behind the installation script is. Any bash monkeys out there want to try and figure that out?

That’s the high level process. Most everything else is obvious from the documentation. Hopefully, this overview helps at least one other PHP developer get up and running with Zephir toolkit and start learning the Zephir language. Just start by creating your project directory and then running

zephir init your project

This will create a directory for yourproject with all the files Zephir needs to build your project. You can then add your first .zep file in the yourproject/yourproject subdirectory and start coding.

Zephir and PHP 7.2 Rule!

I’ve been working with PHP since 2000 and I still enjoy using it. And now, with the release of PHP 7.2, which has all the good parts of PHP 7, including increased performance and a better type hinting system, PHP 7.2 fixes a number of bugs that impact security, such as a broken random number generator that didn’t produce cryptographically useful random values. I think if developers begin utilizing the extension system more often through Zephir, the combination of improvements in PHP 7 and an easier path to developing language extensions, will allow the PHP community to build faster, more robust applications and the PHP platform may regain some of its respectability. Zephir and PHP 7.2 and bring new life to a development platform that deserves more respect than becoming yet another legacy language.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s