Zephir for PHP extension development

Keywords: PHP JSON Web Server github

If you often pay attention to the PHP framework falcon, you should know that the team of Falcon has developed a high-level language for better development of falcon—— Zephir.

Zephir, an open source high-level language, aims to simplify the creation and maintainability of PHP extensions, focusing on type and memory security.

If you don't have the foundation of C/C + + language and need to develop business in the way of PHP extension, you can choose Zephir.
Using Zephir to develop the extended environment is very easy to build, according to Official documents Step, you can quickly get the environment ready. In addition, the latest version of Zephir has provided a. phar file, which can be downloaded and added to the path environment variable to start development.

[root@ef40a04d7af1 ~]# zephir
 _____              __    _
/__  /  ___  ____  / /_  (_)____
  / /  / _ \/ __ \/ __ \/ / ___/
 / /__/  __/ /_/ / / / / / /
/____/\___/ .___/_/ /_/_/_/
         /_/

Zephir 0.11.11 by Andres Gutierrez and Serghei Iakovlev (b661a58)

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --dumpversion     Print the Zephir version — and don't do anything else
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  api        Generates a HTML API based on the classes exposed in the extension
  build      Generates/Compiles/Installs a Zephir extension
  clean      Cleans any object files created by the extension
  compile    Compile a Zephir extension
  fullclean  Cleans any object files created by the extension (including files generated by phpize)
  generate   Generates C code from the Zephir code without compiling it
  help       Displays help for a command
  init       Initializes a Zephir extension
  install    Installs the extension in the extension directory (may require root password)
  list       Lists commands
  stubs      Generates stubs that can be used in a PHP IDE

Let's use Zephir to develop a simple PHP extension - Hutils - to introduce the basic development process.
1. Initialize the extension directory

zephir init Hutils
cd hutils && tree -L 2
//The following directory structure will be generated
.
|-- config.json
|-- ext
|   `-- kernel
`-- hutils

2. Write extension code
Enter the hutils/hutils / directory and create a common.zep file.

namespace Hutils;

class Common
{

    public static function say()
    {
        echo "hello world!";
    }

}

3. Compile extension


zephir build
 Preparing for PHP compilation...
 Preparing configuration file...
 Compiling...
 Installing...

 Extension installed.
 Add "extension=hutils.so" to your php.ini

 ! [NOTE] Don't forget to restart your web server

After adding extension=hutils.so to php.ini, you can call the hutils extension method in PHP code.

[root@ef40a04d7af1 ~]# php -r "echo Hutils\Common::say();"
Hello World!

After the extension development is completed, hutils\ext can also be packaged and published, so that it can be separated from the Zephir development environment and used in general extension compilation and installation steps.

Related learning materials:
1.Zephir document
1.Zephir GitHub

Posted by nvidia on Fri, 06 Dec 2019 12:24:58 -0800