zoukankan      html  css  js  c++  java
  • PHP扩展开发之Zephir

    如果你时常关注PHP框架Phalcon,那么应该知道Phalcon的团队为了更好的开发Phalcon,开发了一门高级语言——Zephir

    Zephir,一种开源的高级语言,旨在简化PHP扩展的创建和可维护性,重点关注类型和内存安全性。
    

    如果你没有C/C++语言的基础,又需要以PHP扩展的方式开发业务,那么可以选择Zephir。
    使用Zephir开发扩展的环境很容易搭建,按照官方文档步骤,可以很快的把环境准备好。而且最新版本的Zephir已经提供.phar文件,下载这个文件添加进path环境变量就能开始开发了。

    [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
    

    下面使用Zephir开发一个简单PHP扩展—Hutils—来介绍基本开发流程。
    1. 初始化扩展目录

    zephir init Hutils
    cd hutils && tree -L 2
    //会生成如下目录结构
    .
    |-- config.json
    |-- ext
    |   `-- kernel
    `-- hutils
    

    2. 编写扩展代码
    进入hutils/hutils/目录,创建一个common.zep文件。

    namespace Hutils;
    
    class Common
    {
    
        public static function say()
        {
            echo "hello world!";
        }
    
    }
    

    3. 编译扩展

    
    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
    

    然后在php.ini中添加extension=hutils.so后,就可以在php代码中调用hutils扩展中的方法了。

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

    扩展开发完成后,也可以将hutilsext打包发布,这样就可以脱离Zephir开发环境,使用一般扩展编译安装步骤使用。

    相关学习资料:
    1.Zephir文档
    1.Zephir GitHub



    作者:就如此过
    链接:https://www.jianshu.com/p/487c7cafdccc
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    sunjiali
    dingding
    xlrd
    Python基础2
    Python常用算法学习
    Python基础1
    分布式监控系统
    堡垒机
    通过Python实现简单的计算器
    Python常用模块学习
  • 原文地址:https://www.cnblogs.com/xiami2046/p/13599938.html
Copyright © 2011-2022 走看看