windows 上使用 zephir 开发php扩展

3 篇文章 0 订阅

转自:http://www.widuu.com/archives/04/1189.html


0x01 介绍

官方上提供了 ubuntu 的,也就是说基本版本的 linux 版本都可以哈,我上一篇文章介绍了 Docker 和 MAC OS 上开发 PHP 扩展,这一章很简单,就是开发windows 上的 php 的扩展。

0x02 环境

大家可以根据我前边的文章 windows 扩展开发php 这篇文章来搭建环境。

Visual Studio 2010 2012
php-sdk-binary-tools-
deps 
php 源码

0x03 zephir windows 环境

  1. 安装 composer ,点击 Composer-Setup.exe 连接,下载安装即可。

  2. 建立一个文件夹,譬如 php , 然后命令行

    g:\
    cd php
    composer require phalcon/zephir:dev-master

这个时候,你需要将地址添加到你的环境变量中,譬如我的

#添加到环境变量中
G:\php\vendor\bin 

0x03

编写源代码,编译,测试

  1. 命令行,创建一个项目

    zephir init widuu
  2. 进入 widuu/widuu 文件夹,建立一个 service.zep 文件。

  3. 编写源代码,如下,就是上一篇文章的例子

    namespace Widuu;
    
    class Service{
    
        protected _service;
    
        public  function _set(string name,object obj) -> int{
            if (typeof obj != "object") {
                throw new \Exception("type error!!");
            }
            let this->_service[name] = obj;
            return 1;
        } 
    
        public  function _get(string name){
            if (!isset this->_service[name]) {
                return 0;
            }
            return this->_service[name];
        }
    
        public  function _del(string name){
            let this->_service[name] = null;
        }
    
    }
  4. 编译安装

    • 使用如下命令

      cd ..  # 这里指,我们用zephir 创建的 widuu 目录
      zephir generate widuu
    • 将 ext 文件夹,复制到开发环境中的扩展目录,并重新命名为 widuu

    注:开发环境的扩展目录,相对于 http://www.widuu.com/archives/12/1150.html 这篇文章中的 C:\php-sdk\phpdev\vc11\x86\php-5.5.20\ext 目录。

    • 然后命令行进入开发环境中的 C:\php-sdk\phpdev\vc11\x86\php-5.5.20\ 目录,执行如下语句

      buildconf --force
      configure --disable-all --enable-cli --enable-widuu=shared 
      #编译
      nmake
  5. 编译完成后,我们就在 C:\php-sdk\phpdev\vc11\x86\php-5.5.20\Release_TS 目录中看到了 php_widuu.dll动态链接库了,然后放到我们的 php 的 ext 文件夹中,并在 php.ini 中加入

    extension = php_widuu.dll

00×4

重启你的环境,测试,用上一篇文章的代码测试,如下

<?php

class string{

    public function test(){
        echo "hello word";
    }
}

$service = new Widuu\Service();
// 故意写错类型
try{
    $service->_set('string',"222");
}catch(Exception $e){
    echo $e->getMessage();
}
// 注册服务
$service->_set('string',new string());
// 获取对象
$s = $service->_get('string');
// 测试
$s->test();      

结果输出

type error! hello word

成功,同样的功能,大家有时间可以分析一下 C 的源代码,如果大家有问题,可以发邮件 admin#widuu.com


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用 Zephir 编写的简单的 PHP 扩展示例: 1. 首先,创建一个名为 `hello` 的文件夹,该文件夹包含以下内容: ``` hello/ ├── ext/ │ └── config.m4 │ └── hello.zep │ └── php_hello.h ├── hello.php └── php.ini ``` 2. 在 `ext` 目录下创建一个名为 `config.m4` 的文件,并将以下内容添加到该文件中: ```m4 PHP_ARG_ENABLE(hello, whether to enable Hello support, [ --enable-hello Enable Hello support]) if test "$PHP_HELLO" = "yes"; then PHP_SUBST(HELLO_SHARED_LIBADD) PHP_NEW_EXTENSION(hello, hello.zep, $ext_shared) fi ``` 3. 在 `ext` 目录下创建一个名为 `hello.zep` 的文件,并将以下内容添加到该文件中: ```zephir namespace Hello; function sayHello() -> void { echo "Hello, world!"; } ``` 4. 在 `ext` 目录下创建一个名为 `php_hello.h` 的文件,并将以下内容添加到该文件中: ```c #ifndef PHP_HELLO_H #define PHP_HELLO_H 1 #include "php.h" extern zend_module_entry hello_module_entry; #define phpext_hello_ptr &hello_module_entry #endif ``` 5. 在 `hello` 目录下创建一个名为 `hello.php` 的文件,并将以下内容添加到该文件中: ```php <?php if (!extension_loaded('hello')) { dl('hello.' . PHP_SHLIB_SUFFIX); } Hello\sayHello(); ``` 6. 在 `hello` 目录下创建一个名为 `php.ini` 的文件,并将以下内容添加到该文件中: ```ini extension=hello ``` 7. 在命令行中导航到 `hello/ext` 目录,并运行以下命令来编译并安装扩展: ``` zephir build sudo make install ``` 8. 在命令行中导航到 `hello` 目录,并运行以下命令来运行 PHP 脚本: ``` php hello.php ``` 输出应该是 `Hello, world!`。 这是一个非常简单的示例,但它可以让您了解如何使用 Zephir 编写 PHP 扩展。您可以使用更复杂的代码来构建更高级的扩展
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值