gpt4 book ai didi

php - Mustache.php 从命名空间内启动 - include() : failed to open stream

转载 作者:行者123 更新时间:2023-12-04 17:02:02 24 4
gpt4 key购买 nike

使用简单的自动加载器时,Mustache.php从命名空间内启动 webpage正确呈现数据,但也会返回一长串有关包含其许多文件失败的警告列表。 Mustache 通过要求其 Autoloader.php 加载,正如其官方推荐的documentation .

我发现了几篇讨论类似问题的帖子( 12 )。典型的解决方案是添加 \前面Mustache_Autoloader::register();将其加载到全局命名空间中。但是,这似乎对我不起作用。

在 Google Cloud PHP 开发服务器 (5.5.26) 和成熟的 PHP 服务器 (7.1.9) 上都会生成相同的警告列表。

最小代码如下。

目录结构:

.
│ index.php
└───app
├───controllers
│ └───webpage
│ home.php
└───vendor
└───Mustache
Autoloader.php
index.php的内容:
function autoloader($class)
{
include($_SERVER['DOCUMENT_ROOT'] . '/app/controllers/' . $class . '.php');
}
spl_autoload_register('autoloader');

new \webpage\home;
home.php的内容:
namespace webpage;

class home
{
function __construct()
{
require $_SERVER['DOCUMENT_ROOT'] . '/app/vendor/Mustache/Autoloader.php';

\Mustache_Autoloader::register();

$mustache = new \Mustache_Engine();

echo $mustache->render('Hello {{planet}}', array('planet' => 'World!'));
}
}

请注意,输出的最底部是一个正确呈现的字符串“Hello World”。
Warning: include(D:/project/app/controllers/Mustache_Engine.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Engine.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Loader_StringLoader.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Loader_StringLoader.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Loader.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Loader.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Cache_NoopCache.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Cache_NoopCache.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Cache_AbstractCache.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Cache_AbstractCache.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Cache.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Cache.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Logger.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Logger.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Parser.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Parser.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Tokenizer.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Tokenizer.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Compiler.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Compiler.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Template.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Template.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_Context.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_Context.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5

Warning: include(D:/project/app/controllers/Mustache_HelperCollection.php): failed to open stream: No such file or directory in D:\project\index.php on line 5

Warning: include(): Failed opening 'D:/project/app/controllers/Mustache_HelperCollection.php' for inclusion (include_path='.;D:\project;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\php\sdk') in D:\project\index.php on line 5
Hello World!

你知道如何解决它吗?谢谢你。

最佳答案

PHP 在尝试加载文件时,会按顺序尝试每个已注册的自动加载器。一旦一个自动加载器成功加载文件,它就会停止尝试。在你的情况下,它正在尝试自动加载器 index.php在尝试使用 Mustache 自动加载器之前。

以及 index.php 中的自动加载器太天真了:

function autoloader($class)
{
include($_SERVER['DOCUMENT_ROOT'] . '/app/controllers/' . $class . '.php');
}
spl_autoload_register('autoloader');

这段代码盲目调用 include使用它给出的任何类名,无论这样的文件是否存在。您可以从输出中看到它试图在 controllers 中包含一个文件对于 Mustache 使用的每个类。

代码不会完全失败,因为在您的自动加载器无法加载文件后,PHP 会尝试下一个注册的自动加载器(Mustache 附带的那个)并成功加载必要的文件。

您可以通过在包含文件之前检查文件是否存在来修复自动加载器代码。你也可以通过检查 webpage\ 让它更聪明一点。在尝试加载文件之前类上的命名空间前缀。

关于php - Mustache.php 从命名空间内启动 - include() : failed to open stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47486218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com