使用data://text/plain在php中包含文件时


Using data://text/plain when including files in php

我正在尝试require_once()一个PHP代码字符串。它工作得很好,除了在使用名称空间的类时,这总是会导致致命的错误。我不确定我做错了什么:

require_once('data://text/plain, <?php namespace Foo'Bar; use 'Somestuff; class Baz {...}

错误消息也在包含的代码中间开始输出,这似乎很重要:

Fatal error: Namespace declaration statement has to be the very first statement in the script in data://text/plain, $prop; } throw new InvalidArgumentException('

代码"$prop;}throw new InvalidArgumentException('"是我试图包含的类中的几行代码

我在5.4和5.6上都试过了,结果都一样。

正确的方法是什么?欢迎提出任何想法。

逗号后面和打开的<?php前面有一个空格。逗号后面的所有内容都是"输出"。空间可能不会破坏所有代码,但显然namespace和任何会话使用等都会破坏。删除空间:

require_once('data://text/plain,<?php namespace Foo'Bar; use 'Somestuff; class Baz {}');