3

Using vfsStream, am I able to require or include a virtual file?

$structure = array(
    'classes' => array('Foo.php' => '<?php class Foo {} ?>')
);
\vfsStream::create($structure);

require_once(\vfsStream::url('classes').DIRECTORY_SEPARATOR.'Foo.php');

The code above fails silently under PHPUnit.

Thanks.

thom
  • 31
  • 2
  • https://bugs.php.net/bug.php?id=50898 – Marek Sebera Sep 30 '11 at 19:19
  • 1
    I'm having the same issue. The PHP documentation says that include/require accepts streams, so it seems that this should work. I've filed a [bug report with vfsStream](https://github.com/mikey179/vfsStream/issues/22) – Michael Oct 03 '11 at 13:18

2 Answers2

1

Have you tried require_once(\vfsStream::url('root/classes').DIRECTORY_SEPARATOR.'Foo.php'); ? The call to vfsStream::create($structure); creates the root directory, and does not use the first entry in $structures as the root directory as there might be more then one element in this array. See also documentation at https://github.com/mikey179/vfsStream/wiki/Createcomplexstructures.

1

In addition to Frank's answer about the incorrect use of url(), there may be a configuration issue. In a stock PHP installation, you have to make sure allow_url_fopen is enabled in your php.ini and allow_url_include is enabled in configuration or your script.

In my case, however, I'm running the Suhosin extension, which ignores these parameters and completely disables url_fopen by default. In order to include/require a vfsStream file, you need to add the vfs:// scheme to Suhosin's whitelist in php.ini: suhosin.executor.include.whitelist = "vfs://"

Thanks to Frank Kleine, the vfsStream maintainer, for helping me track this down.1

Michael
  • 548
  • 4
  • 11
  • This was an absolute lifesaver. After an OS and PHP version upgrade, require_once with a http:// URL just failed silently. No logs, no error messages, nothing. I was racking my brains trying to work out what was happening. Enabled the appropriate schemata in suhosin.ini et voila! Now we're back up and I look like a genius. – David G Nov 15 '12 at 20:51