Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, so the way I would suggest doing this is with a file <a href="http://us2.php.net/manual/en/class.streamwrapper.php" rel="noreferrer">stream wrapper</a>. Let me whip up a quick example:</p> <pre><code>class staticStreamWrapper { public $context; protected static $data = array(); protected $path = ''; protected $pointer = 0; protected $writable = false; public function stream_close() {} public function stream_eof() { return $this-&gt;pointer &gt;= strlen(static::$data[$this-&gt;path]); } public function stream_flush() {} public function stream_open($path, $mode, $options, &amp;$opened_path) { switch ($mode[0]) { case 'r': if (!isset(static::$data[$path])) return false; $this-&gt;path = $path; $this-&gt;writable = isset($mode[1]) &amp;&amp; $mode[1] == '+'; break; case 'w': static::$data[$path] = ''; $this-&gt;path = $path; $this-&gt;writable = true; break; case 'a': if (!isset(static::$data[$path])) static::$data[$path] = ''; $this-&gt;path = $path; $this-&gt;writable = true; $this-&gt;pointer = strlen(static::$data[$path]); break; case 'x': if (isset(static::$data[$path])) return false; $this-&gt;path = $path; $this-&gt;writable = true; break; case 'c': if (!isset(static::$data[$path])) static::$data[$path] = ''; $this-&gt;path = $path; $this-&gt;writable = true; break; default: return false; } $opened_path = $this-&gt;path; return true; } public function stream_read($count) { $bytes = min(strlen(static::$data[$this-&gt;path]) - $this-&gt;pointer, $count); $data = substr(static::$data[$this-&gt;path], $this-&gt;pointer, $bytes); $this-&gt;pointer += $bytes; return $data; } public function stream_seek($offset, $whence = SEEK_SET) { $len = strlen(static::$data[$this-&gt;path]); switch ($whence) { case SEEK_SET: if ($offset &lt;= $len) { $this-&gt;pointer = $offset; return true; } break; case SEEK_CUR: if ($this-&gt;pointer + $offset &lt;= $len) { $this-&gt;pointer += $offset; return true; } break; case SEEK_END: if ($len + $offset &lt;= $len) { $this-&gt;pointer = $len + $offset; return true; } break; } return false; } public function stream_stat() { $size = strlen(static::$data[$this-&gt;path]); $time = time(); return array( 0 =&gt; 0, 'dev' =&gt; 0, 1 =&gt; 0, 'ino' =&gt; 0, 2 =&gt; 0777, 'mode' =&gt; 0777, 3 =&gt; 1, 'nlink' =&gt; 1, 4 =&gt; 0, 'uid' =&gt; 0, 5 =&gt; 0, 'gid' =&gt; 0, 6 =&gt; '', 'rdev' =&gt; '', 7 =&gt; $size, 'size' =&gt; $size, 8 =&gt; $time, 'atime' =&gt; $time, 9 =&gt; $time, 'mtime' =&gt; $time, 10 =&gt; $time, 'ctime' =&gt; $time, 11 =&gt; -1, 'blksize' =&gt; -1, 12 =&gt; -1, 'blocks' =&gt; -1, ); } public function stream_tell() { return $this-&gt;pointer; } public function stream_write($data) { if (!$this-&gt;writable) return 0; $size = strlen($data); $len = strlen(static::$data[$this-&gt;path]); if ($this-&gt;stream_eof()) { static::$data[$this-&gt;path] .= $data; } else { static::$data[$this-&gt;path] = substr_replace( static::$data[$this-&gt;path], $data, $this-&gt;pointer ); } $this-&gt;pointer += $size; return $size; } public function unlink($path) { if (isset(static::$data[$path])) { unset(static::$data[$path]); } return true; } } </code></pre> <p>Now, you would then need to register the wrapper:</p> <pre><code>stream_wrapper_register('static', 'staticStreamWrapper'); </code></pre> <p>So you now can treat it like a file even though it never actually leaves PHP (it's stored as a static variable)!</p> <pre><code>file_put_contents('static://foo.txt', 'this is my data'); file_get_contents('static://foo.txt'); // "this is my data" $f = fopen('static://foo.txt', 'r'); // should return a resource // etc... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload