Build PHP Extensions Easily with Zephir

0

Zephir – Ze(nd Engine) Ph(p) I(nt)r(mediate) – is a high level language that eases the creation and maintainability of PHP extensions. Zephir extensions are exported to C code that can be compiled and optimized by major C compilers such as gcc/clang/vc++. Functionality is exposed to the PHP language.

Zephir combines static and dynamic typing in a friendly language that is compiled down to machine code using the industry standard compilers. Despite being a compiled language, it does not allow you to use pointers, it provides a garbage collector to avoid memory leaks and more.

Build PHP Extension with Zephir

Build PHP Extensions with Zephir

It takes advantage of the extension ecosystem available in PHP to allow you create your own extensions as shared libraries in Linux/OSX and DLLs on Windows.

The code below registers a class with a method that filters variables, returning their alphabetic characters:

namespace MyLibrary;

/**
 * Filter
 */
class Filter
{
    /**
     * Filters a string, returning its alpha characters
     *
     * @param string str
     */
    public function alpha(string str)
    {
        char ch; string filtered = "";

        for ch in str {
           if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
              let filtered .= ch;
           }
        }
        return filtered;
    }
}

The class can be used from PHP as follows:

$filter = new MyLibrary\Filter();
echo $filter->alpha("01he#l.lo?/1"); // prints hello

More information and download at https://zephir-lang.com/en

LEAVE A REPLY

Please enter your comment!
Please enter your name here