= 4.3.2, PHP 5, PHP 7, PHP 8)stream_wrapper_register — 注冊(cè)一個(gè)用 PHP 類實(shí)現(xiàn)的 URL 封裝協(xié)議說明stream_wrapper_register(string $protoco">

stream_wrapper_register

(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)

stream_wrapper_register注冊(cè)一個(gè)用 PHP 類實(shí)現(xiàn)的 URL 封裝協(xié)議

說明

stream_wrapper_register(string $protocol, string $class, int $flags = 0): bool

允許用戶實(shí)現(xiàn)自定義的協(xié)議處理器和流,用于所有其它的文件系統(tǒng)函數(shù)中(例如 fopen(),fread() 等)。

參數(shù)

protocol

待注冊(cè)的封裝的名字。有效的協(xié)議名字必須只包含字母數(shù)字、點(diǎn)(.)、加號(hào)(+)、連字符(-)。

class

實(shí)現(xiàn)了 protocol 的類名。

flags

如果 protocol 是一個(gè) URL 協(xié)議,應(yīng)該設(shè)置為 STREAM_IS_URL。默認(rèn)為 0,本地流。

返回值

成功時(shí)返回 true, 或者在失敗時(shí)返回 false

當(dāng) protocol 已經(jīng)有處理者時(shí),stream_wrapper_register() 將返回false

范例

示例 #1 如何注冊(cè)一個(gè) stream wrapper

<?php
$existed 
in_array("var"stream_get_wrappers());
if (
$existed) {
    
stream_wrapper_unregister("var");
}
stream_wrapper_register("var""VariableStream");
$myvar "";

$fp fopen("var://myvar""r+");

fwrite($fp"line1\n");
fwrite($fp"line2\n");
fwrite($fp"line3\n");

rewind($fp);
while (!
feof($fp)) {
    echo 
fgets($fp);
}
fclose($fp);
var_dump($myvar);

if (
$existed) {
    
stream_wrapper_restore("var");
}

?>

以上例程會(huì)輸出:

line1
line2
line3
string(18) "line1
line2
line3
"

參見