# 15.1 php流的表象之下
# 15.1 php流的表象之下
對于給定的流實例, 比如文件流和網絡流, 它們的不同在于上一章你使用的流創建函數返回的php\_stream結構體中的ops成員.
```
typedef struct _php_stream {
...
php_stream_ops *ops;
...
} php_stream;
```
php\_stream\_ops結構體定義的是一個函數指針集合以及一個描述標記.
```
typedef struct _php_stream_ops {
size_t (*write)(php_stream *stream, const char *buf,
size_t count TSRMLS_DC);
size_t (*read)(php_stream *stream, char *buf,
size_t count TSRMLS_DC);
int (*close)(php_stream *stream, int close_handle
TSRMLS_DC);
int (*flush)(php_stream *stream TSRMLS_DC);
const char *label;
int (*seek)(php_stream *stream, off_t offset, int whence,
off_t *newoffset TSRMLS_DC);
int (*cast)(php_stream *stream, int castas, void **ret
TSRMLS_DC);
int (*stat)(php_stream *stream, php_stream_statbuf *ssb
TSRMLS_DC);
int (*set_option)(php_stream *stream, int option,int value,
void *ptrparam TSRMLS_DC);
} php_stream_ops;
```
當流訪問函數比如php\_stream\_read()被調用時, 流包裝層實際上解析調用了stream->ops中對應的函數, 這樣實際調用的就是當前流類型特有的read實現. 比如, 普通文件的流ops結構體中的read函數實現如下(實際的該實現比下面的示例復雜一點):
```
size_t php_stdio_read(php_stream *stream, char *buf,
size_t count TSRMLS_DC)
{
php_stdio_stream_data *data =
(php_stdio_stream_data*)stream->abstract;
return read(data->fd, buf, count);
}
```
而compress.zlib流使用的ops結構體中則read則指向的是如下的函數:
```
size_t php_zlib_read(php_stream *stream, char *buf,
size_t count TSRMLS_DC)
{
struct php_gz_stream_data_t *data =
(struct php_gz_stream_data_t *) stream->abstract;
return gzread(data->gz_file, buf, count);
}
```
這里第一點需要注意的是ops結構體指向的函數指針常常是對數據源真正的讀取函數的一個瘦代理. 在上面兩個例子中, 標準I/O流使用posix的read()函數, 而zlib流使用的是libz的gzread()函數.
你可能還注意到了, 這里使用了stream->abstract元素. 這是流實現的一個便利指針, 它可以被用于獲取各種相關的捆綁信息. 在上面的例子中, 指向自定義結構體的指針, 用于存儲底層read函數要使用的文件描述符.
還有一件你可能注意到的事情是php\_stream\_ops結構體中的每個函數都期望一個已有的流實例, 但是怎樣得到實例呢? abstract成員是怎樣設置的以及什么時候流指示使用哪個ops結構體? 答案就在你在上一章使用過的第一個打開流的函數(php\_stream\_open\_wrapper())中.
當這個函數被調用時, php的流包裝層嘗試基于傳遞的URL中的scheme://部分確定請求的是什么協議. 這樣它就可以在已注冊的php包裝器中查找對應的php\_stream\_wrapper項. 每個php\_stream\_wrapper結構體都可以取到自己的ops元素, 它指向一個php\_stream\_wrapper\_ops結構體:
```
typedef struct _php_stream_wrapper_ops {
php_stream *(*stream_opener)(php_stream_wrapper *wrapper,
char *filename, char *mode,
int options, char **opened_path,
php_stream_context *context
STREAMS_DC TSRMLS_DC);
int (*stream_closer)(php_stream_wrapper *wrapper,
php_stream *stream TSRMLS_DC);
int (*stream_stat)(php_stream_wrapper *wrapper,
php_stream *stream,
php_stream_statbuf *ssb
TSRMLS_DC);
int (*url_stat)(php_stream_wrapper *wrapper,
char *url, int flags,
php_stream_statbuf *ssb,
php_stream_context *context
TSRMLS_DC);
php_stream *(*dir_opener)(php_stream_wrapper *wrapper,
char *filename, char *mode,
int options, char **opened_path,
php_stream_context *context
STREAMS_DC TSRMLS_DC);
const char *label;
int (*unlink)(php_stream_wrapper *wrapper, char *url,
int options,
php_stream_context *context
TSRMLS_DC);
int (*rename)(php_stream_wrapper *wrapper,
char *url_from, char *url_to,
int options,
php_stream_context *context
TSRMLS_DC);
int (*stream_mkdir)(php_stream_wrapper *wrapper,
char *url, int mode, int options,
php_stream_context *context
TSRMLS_DC);
int (*stream_rmdir)(php_stream_wrapper *wrapper, char *url,
int options,
php_stream_context *context
TSRMLS_DC);
} php_stream_wrapper_ops;
```
這里, 流包裝層調用wrapper->ops->stream\_opener(), 它將執行包裝器特有的操作創建流實例, 賦值恰當的php\_stream\_ops結構體, 綁定相關的抽象數據.
dir\_opener()函數和stream\_opener()提供相同的基礎服務; 不過, 它是對php\_stream\_opendir()這個API調用的響應, 并且通常會綁定一個不同的php\_stream\_ops結構體到返回的實例. stat()和close()函數在這一層上是重復的, 這樣做是為了給包裝器的這些操作增加協議特有的邏輯.
其他的函數則允許執行靜態流操作而不用實際的創建流實例. 回顧這些流API調用, 它們并不實際返回php\_stream對象, 你馬上就會看到它們的細節.
> 盡管在php 4.3中引入流包裝層時, url\_stat在內部作為一個包裝器的ops函數存在, 但直到php 5.0它才開始被使用. 此外, 最后的3個函數, rename(), stream\_mkdir()以及stream\_rmdir()一直到php 5.0才引入, 在這個版本之前, 它們并不在包裝器的ops結構中.
## links
- [目錄](preface.md)
- 15 [流的實現](15.html)
- 15.2 [包裝器操作](15.2.html)
- 介紹
- 1 PHP的生命周期
- 1.1 讓我們從SAPI開始
- 1.2 PHP的啟動與終止
- 1.3 PHP的生命周期
- 1.4 線程安全
- 1.5 PHP的生命周期
- 2 PHP變量在內核中的實現
- 2.1 變量的類型
- 2.2 變量的值
- 2.3 創建PHP變量
- 2.4 變量的存儲方式
- 2.5 變量的檢索
- 2.6 類型轉換
- 2.7 小結
- 3 內存管理
- 3.1 內存管理
- 3.2 引用計數
- 3.3 內存管理
- 4 動手編譯PHP
- 4.1 動手編譯PHP
- 4.2 動手編譯PHP
- 4.3 Unix/Linux平臺下的編譯
- 4.4 在Win32平臺上編譯PHP
- 4.5 動手編譯PHP
- 5 Your First Extension
- 5.1 Your First Extension
- 5.2 編譯我們的擴展
- 5.3 靜態編譯
- 5.4 編寫函數
- 5.5 Your First Extension
- 6 函數返回值
- 6.1 函數返回值
- 6.2 引用與函數的執行結果
- 6.3 函數返回值
- 7 函數的參數
- 7.1 函數的參數
- 7.2 函數的參數
- 7.3 函數的參數
- 8 使用HashTable與{數組}
- 8.1 使用HashTable與{數組}
- 8.2 使用HashTable與{數組}
- 8.3 使用HashTable與{數組}
- 8.4 使用HashTable與{數組}
- 9 PHP中的資源類型
- 9.1 PHP中的資源類型
- 9.2 PHP中的資源類型
- 9.3 PHP中的資源類型
- 9.4 PHP中的資源類型
- 10 PHP中的面向對象(一)
- 10.1 PHP中的面向對象(一)
- 10.2 PHP中的面向對象(一)
- 10.3 PHP中的面向對象(一)
- 10.4 PHP中的面向對象(一)
- 10.5 PHP中的面向對象(一)
- 11 PHP中的面向對象(二)
- 11.1 PHP中的面向對象(二)
- 11.2 PHP中的面向對象(二)
- 11.3 PHP中的面向對象(二)
- 12 啟動與終止的那點事
- 12.1 關于生命周期
- 12.2 MINFO與phpinfo
- 12.3 常量
- 12.4 PHP擴展中的全局變量
- 12.5 PHP語言中的超級全局變量(Superglobals)
- 12.6 小結
- 13 INI設置
- 13.1 聲明和訪問INI設置
- 13.2 小結
- 14 流式訪問
- 14.1 流的概覽
- 14.2 訪問流
- 14.3 靜態資源操作
- 14.4 links
- 15 流的實現
- 15.1 php流的表象之下
- 15.2 包裝器操作
- 15.3 實現一個包裝器
- 15.4 操縱
- 15.5 檢查
- 15.6 小結
- 16 有趣的流
- 16.1 上下文
- 16.2 過濾器
- 16.3 小結
- 17 配置和鏈接
- 17.1 autoconf
- 17.2 庫的查找
- 17.3 強制模塊依賴
- 17.4 Windows方言
- 17.5 小結
- 18 擴展生成
- 18.1 ext_skel
- 18.2 PECL_Gen
- 18.3 小結
- 19 設置宿主環境
- 19.1 嵌入式SAPI
- 19.2 構建并編譯一個宿主應用
- 19.3 通過嵌入包裝重新創建cli
- 19.4 老技術新用
- 19.5 小結
- 20 高級嵌入式
- 20.1 回調到php中
- 20.2 錯誤處理
- 20.3 初始化php
- 20.4 覆寫INI_SYSTEM和INI_PERDIR選項
- 20.5 捕獲輸出
- 20.6 同時擴展和嵌入
- 20.7 小結