码迷,mamicode.com
首页 > Web开发 > 详细

PHP 封装自己的协议

时间:2017-01-24 13:40:14      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:write   pre   cti   strlen   return   false   set   fopen   fail   

  1 <?php
  2 /**
  3  * 引用:http://php.net/manual/en/function.stream-wrapper-register.php
  4  * 把变量当成文件读写的协议
  5  *
  6  * Class VariableStream
  7  */
  8 class VariableStream
  9 {
 10     private $postition;   // 当前的位置
 11     private $varname;     // 变量的名字
 12 
 13     /**
 14      * 打开
 15      *
 16      * @param $path
 17      * @param $mode
 18      * @param $options
 19      * @param $opened_path
 20      * @return bool
 21      */
 22     public function stream_open($path, $mode, $options, &$opened_path)
 23     {
 24         $url = parse_url($path);
 25 
 26         $this->varname   = $url[‘host‘];
 27         $this->postition = 0;
 28 
 29         return true;
 30     }
 31 
 32     /**
 33      * 读取
 34      *
 35      * @param $count
 36      * @return string
 37      */
 38     public function stream_read($count)
 39     {
 40 
 41         $p   = $this->postition;
 42         $ret = substr($GLOBALS[$this->varname], $p,$count);
 43         $p  += $count;
 44         return $ret;
 45     }
 46 
 47     /**
 48      * 写入
 49      *
 50      * @param $data
 51      * @return int
 52      */
 53     public function stream_write($data)
 54     {
 55         $v = &$GLOBALS[$this->varname];
 56         $l = strlen($data);
 57         $p = &$this->postition;
 58         $v = substr($v,0,$p) .$data.substr($v,$p+=$l);
 59 
 60         return $l;
 61     }
 62 
 63     /**
 64      * 返回位置
 65      *
 66      * @return mixed
 67      */
 68     public function stream_tell()
 69     {
 70         return $this->postition;
 71     }
 72 
 73     /**
 74      * 寻位
 75      *
 76      * @param $offset
 77      * @param $whence
 78      * @return bool
 79      */
 80     public function stream_seek($offset, $whence)
 81     {
 82         $dp = $p = &$this->postition;
 83         $l  = strlen($GLOBALS[$this->varname]);
 84 
 85         switch($whence)
 86         {
 87             case SEEK_SET:
 88                 $p = 0;
 89                 break;
 90             case SEEK_END:
 91                 $p = $l;
 92                 break;
 93             default:
 94                 return false;
 95         }
 96         $p += $offset;
 97         $ret = ($p >= 0 || $p <= $l);
 98         if(!$ret)
 99             $this->postition = $dp;
100         return $ret;
101     }
102 
103     public function stream_eof()
104     {
105         return strlen($GLOBALS[$this->varname])== $this->postition;
106     }
107 }
108 
109 // 撤销已注册的同名协议
110 if(in_array(‘var‘,stream_get_wrappers()))
111 {
112     stream_wrapper_unregister(‘var‘);
113 }
114 // 注册自己的协议
115 stream_wrapper_register(‘var‘,‘VariableStream‘) or die(‘Failed to register protocol‘);
116 
117 $myvar = "";
118 $fp = fopen(‘var://myvar‘,‘r+‘);
119 if(!$fp){
120     die(‘Failed to open the file!‘);
121 }
122 $str = "Hello the world!";
123 fwrite($fp,$str,strlen($str));
124 
125 echo ‘The length of the string is ‘ .strlen($myvar) ."\n";
126 echo ‘The value of variable ,$myvar,is :‘ .$myvar ."\n";
127 echo ‘The position of the cursor is :‘ .ftell($fp) ."\n";
128 echo ‘Is at the end of the file :‘ .(feof($fp) ? ‘true‘:‘false‘) ."\n";
129 if(fseek($fp,-16,SEEK_END))
130 {
131     echo ‘Now,the position is :‘ .ftell($fp);
132 }

 

PHP 封装自己的协议

标签:write   pre   cti   strlen   return   false   set   fopen   fail   

原文地址:http://www.cnblogs.com/jingjingdidunhe/p/6346884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!