title: How to create a fake / "virtual" file?
body: <p>I am trying to create a "virtual" file without using either memory or a temporary file. The "virtual" file needs to pass a check made with <code>file_exists()</code> while not throwing any error or warning when used with <code>require</code> or <code>include</code>.</p>
  
  <blockquote>
    <p>Allows you to implement your own protocol handlers and streams for use with <a href="http://php.net/manual/en/ref.filesystem.php" rel="nofollow">all the other filesystem functions</a> (such as <code>fopen()</code>, <code>fread()</code> etc.).</p>
  </blockquote>
  
  <p>…where <code>file_exists()</code> is one of them. <a href="http://php.net/manual/en/function.file-exists.php" rel="nofollow">The docs page</a> states:</p>
  
  <blockquote>
    <p>As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to <a href="http://php.net/manual/en/wrappers.php" rel="nofollow">Supported Protocols and Wrappers</a> to determine which wrappers support <a href="http://php.net/manual/en/function.stat.php" rel="nofollow"><code>stat()</code></a> family of functionality.</p>
  </blockquote>
  
  <p>My attempt there was to build a custom, virtual file wrapper</p>
  
  <pre><code>class VirtualFileWrapper
  {
      public $context;
  
      public function stream_open( $path, $mode, $options, &amp;$opened_path )
      {
          return TRUE;
      }
  
      public function stream_stat()
      {
          var_dump( __METHOD__ );
          $data = [
              'dev'     =&gt; 0,
              'ino'     =&gt; getmyinode(),
              'mode'    =&gt; 'r',
              'nlink'   =&gt; 0,
              'uid'     =&gt; getmyuid(),
              'gid'     =&gt; getmygid(),
              'rdev'    =&gt; 0,
              'size'    =&gt; 0,
              'atime'   =&gt; time(),
              'mtime'   =&gt; getlastmod(),
              'ctime'   =&gt; FALSE,
              'blksize' =&gt; 0,
              'blocks'  =&gt; 0,
          ];
          return array_merge( array_values( $data ), $data );
      }
  }
  
  stream_wrapper_register( 'virtual', 'VirtualFileWrapper' );
  
  $file = fopen( "virtual://foo", 'r+' );
  
  // Executes VirtualFileWrapper::stream_stat()
  fstat( $file );
  
  // Executes no VirtualFileWrapper method
  file_exists( $file );
  </code></pre>
  
  <p>While the <code>fstat()</code> function executes the method, <code>file_exists()</code> does not execute any stream class method.</p>
  
  <p>How could I get a virtual stream wrapper to work (with <code>file_exists()</code>)?</p>
  
  <hr>
  
  <p>I am fully aware that <code>tempnam( __DIR__, '' )</code> will pass both:</p>
  
  <ul>
  <li><code>var_dump( tempnam( __DIR__, '' ) );</code> Returns <code>true</code></li>
  <li><code>require tempnam( __DIR__, '' );</code> No error</li>
  </ul>
  
  <p>but I do not want to use a temporary file as there might be a better way (performance wise).</p>