Mustache.php 从命名空间内启动 - include():无法打开流

Mustache.php initiated from within a namespace - include(): failed to open stream

使用简单的自动加载器时,Mustache.php initiated from within a namespace webpage renders the data properly but also returns a long list of warnings about failures to include many of its files. Mustache is loaded by requiring its Autoloader.php, as recommended by its official documentation

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

在 Google 云 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 在尝试加载文件时按顺序尝试每个注册的自动加载器。一旦一个自动加载器成功加载文件,它就会停止尝试。在你的情况下,它在尝试 Mustache 自动加载器之前先在 index.php 中尝试自动加载器。

而且 index.php 中的自动加载器太幼稚了:

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

此代码盲目地调用 include 并使用其指定的任何 class 名称,无论此类文件是否存在。您可以从输出中看到,它试图为 Mustache 使用的每个 class 在 controllers 中包含一个文件。

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

您可以通过在包含文件之前检查文件是否存在来修复自动加载器代码。您还可以通过在尝试加载文件之前检查 class 上的 webpage\ 名称空间前缀来使其更智能。