首页 > 开发 > PHP > 正文

PHP Wrapper在SAE上的应用方法

2024-05-04 22:30:53
字体:
来源:转载
供稿:网友

本文讲述了PHP Wrapper在SAE上的应用方法。分享给大家供大家参考,具体如下:

一、PHP Wrapper是什么

自PHP 4.3开始,PHP开始允许用户通过stream_wrapper_register()自定义URL风格的协议。用户使用fopen(), copy()等文件系统函数对封装协议进行操作时,PHP会调用注册协议时所提供的类中相应的函数。
PHP手册中给了一个例子,它将VariableStream类注册为var://协议,通过这个协议,用户可以使用文件系统函数直接读写全局变量。例如,用户可以通过 “var://foo” 读写 $GLOBALS['foo'] 。

二、SAE为什么需要PHP Wrapper

出于性能和安全方面的考虑,SAE平台上禁用了本地文件读写和对外的数据抓取。相应的,我们提供了对应的服务来做同样的事情。

由于新服务的接口和PHP本身的接口不太一样,专门为我们平台开发的程序当然不会存在问题,但是大量已有的程序和开源项目,就面临着繁杂的迁移工作。而使用PHP Wrapper对我们的服务的接口进行封装之后,用户就可以更方便地将程序迁移到SAE平台。

三、如何写PHP Wrapper

要通过PHP Wrapper封装一个协议,首先,我们需要写一个 streamWrapper 类,类名可自定义,类的格式为:

streamWrapper {public resource $context ;__construct ( void )public bool dir_closedir ( void )public bool dir_opendir ( string $path , int $options )public string dir_readdir ( void )public bool dir_rewinddir ( void )public bool mkdir ( string $path , int $mode , int $options )public bool rename ( string $path_from , string $path_to )public bool rmdir ( string $path , int $options )public resource stream_cast ( int $cast_as )public void stream_close ( void )public bool stream_eof ( void )public bool stream_flush ( void )public bool stream_lock ( mode $operation )public bool stream_open ( string $path , string $mode , int $options , string &$opened_path )public string stream_read ( int $count )public bool stream_seek ( int $offset , int $whence = SEEK_SET )public bool stream_set_option ( int $option , int $arg1 , int $arg2 )public array stream_stat ( void )public int stream_tell ( void )public int stream_write ( string $data )public bool unlink ( string $path )public array url_stat ( string $path , int $flags )}

类中各方法说明:

streamWrapper::__construct — 构造函数,仅在stream_open前被调用
streamWrapper::dir_closedir — 关闭目录句柄,响应closedir()函数
streamWrapper::dir_opendir — 打开目录句柄,响应opendir()函数
streamWrapper::dir_readdir — 从目录句柄读取条目,响应readdir()函数
streamWrapper::dir_rewinddir — 倒回目录句柄,响应rewinddir()函数
streamWrapper::mkdir — 创建目录,响应mkdir()函数
streamWrapper::rename — 目录或文件重命名,响应rename()函数
streamWrapper::rmdir — 删除目录,响应rmdir()函数

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表