【问题标题】:Fatal error: Interface 'Zend\Config\Reader\ReaderInterface'致命错误:接口 'Zend\Config\Reader\ReaderInterface'
【发布时间】:2013-05-25 18:18:37
【问题描述】:
<?php
require_once BLOCKS_ROOT_ENGINE . "Zend/Loader/StandardAutoloader.php";
//require_once BLOCKS_ROOT_ENGINE."library/Zend/Config/Reader/Xml.php";

class Engine {
    static public function start() {
        $autoLoader = new Zend\Loader\StandardAutoloader(array(
            'fallback_autoloader' => true,
        ));
        // register our StandardAutoloader with the SPL autoloader
        $autoLoader->register();


        $config = new Engine_Zend_Config_Reader_Xml();
        //echo $config->config->layout->default->head;
        $root = new Core_Root_Blocks_Root();
    }
}

以上地址位于Engine/Engine.php

Zend 库位于Engine/Zend/* 自动加载器成功找到Xml 类。但由于某种奇怪的原因,它找不到它实现的接口。我究竟做错了什么。

目录结构

XML.php

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Config\Reader;

use XMLReader;
use Zend\Config\Exception;

/**
 * XML config reader.
 */
class Xml implements ReaderInterface
{
    /**
     * XML Reader instance.
     *
     * @var XMLReader
     */
    protected $reader;

    /**
     * Directory of the file to process.
     *
     * @var string
     */
    protected $directory;

    /**
     * Nodes to handle as plain text.
     *
     * @var array
     */
    protected $textNodes = array(
        XMLReader::TEXT,
        XMLReader::CDATA,
        XMLReader::WHITESPACE,
        XMLReader::SIGNIFICANT_WHITESPACE
    );

    /**
     * fromFile(): defined by Reader interface.
     *
     * @see    ReaderInterface::fromFile()
     * @param  string $filename
     * @return array
     * @throws Exception\RuntimeException
     */
    public function fromFile($filename)
    {
        if (!is_file($filename) || !is_readable($filename)) {
            throw new Exception\RuntimeException(sprintf(
                "File '%s' doesn't exist or not readable",
                $filename
            ));
        }

        $this->reader = new XMLReader();
        $this->reader->open($filename, null, LIBXML_XINCLUDE);

        $this->directory = dirname($filename);

        set_error_handler(
            function ($error, $message = '', $file = '', $line = 0) use ($filename) {
                throw new Exception\RuntimeException(sprintf(
                    'Error reading XML file "%s": %s',
                    $filename, $message
                ), $error);
            }, E_WARNING
        );
        $return = $this->process();
        restore_error_handler();

        return $return;
    }

    /**
     * fromString(): defined by Reader interface.
     *
     * @see    ReaderInterface::fromString()
     * @param  string $string
     * @return array|bool
     * @throws Exception\RuntimeException
     */
    public function fromString($string)
    {
        if (empty($string)) {
            return array();
        }
        $this->reader = new XMLReader();

        $this->reader->xml($string, null, LIBXML_XINCLUDE);

        $this->directory = null;

        set_error_handler(
            function ($error, $message = '', $file = '', $line = 0) {
                throw new Exception\RuntimeException(sprintf(
                    'Error reading XML string: %s',
                    $message
                ), $error);
            }, E_WARNING
        );
        $return = $this->process();
        restore_error_handler();

        return $return;
    }

    /**
     * Process data from the created XMLReader.
     *
     * @return array
     */
    protected function process()
    {
        return $this->processNextElement();
    }

    /**
     * Process the next inner element.
     *
     * @return mixed
     */
    protected function processNextElement()
    {
        $children = array();
        $text     = '';

        while ($this->reader->read()) {
            if ($this->reader->nodeType === XMLReader::ELEMENT) {
                if ($this->reader->depth === 0) {
                    return $this->processNextElement();
                }

                $attributes = $this->getAttributes();
                $name       = $this->reader->name;

                if ($this->reader->isEmptyElement) {
                    $child = array();
                } else {
                    $child = $this->processNextElement();
                }

                if ($attributes) {
                    if (!is_array($child)) {
                        $child = array();
                    }

                    $child = array_merge($child, $attributes);
                }

                if (isset($children[$name])) {
                    if (!is_array($children[$name]) || !array_key_exists(0, $children[$name])) {
                        $children[$name] = array($children[$name]);
                    }

                    $children[$name][] = $child;
                } else {
                    $children[$name] = $child;
                }
            } elseif ($this->reader->nodeType === XMLReader::END_ELEMENT) {
                break;
            } elseif (in_array($this->reader->nodeType, $this->textNodes)) {
                $text .= $this->reader->value;
            }
        }

        return $children ?: $text;
    }

    /**
     * Get all attributes on the current node.
     *
     * @return array
     */
    protected function getAttributes()
    {
        $attributes = array();

        if ($this->reader->hasAttributes) {
            while ($this->reader->moveToNextAttribute()) {
                $attributes[$this->reader->localName] = $this->reader->value;
            }

            $this->reader->moveToElement();
        }

        return $attributes;
    }
}

错误

【问题讨论】:

  • 所以您是说Engine_Zend_Config_Reader_Xml 类已成功自动加载,但它实现的ZF 接口不是?您能否提供更多有关您应用的文件结构以及包含路径上的文件夹的信息?
  • @TimFountain 嘿,蒂姆,感谢您的回复,我不在电脑前,但我会尽快将这些信息发送给您。截至目前,我能给你的答案是肯定的。我得到的错误是找不到 Reader_Xml 实现的接口类
  • 我提供了更多细节,包括错误信息和目录结构
  • 您是否尝试过在您的bootstrap.php 中使用$loader-&gt;registerNamespace('Reader') 而不是使用namespace
  • 你能把Engine_Zend_Config_Reader_Xml类的第一部分也发上来吗?

标签: php zend-framework frameworks xmlreader


【解决方案1】:

我认为您错过了在 index.php 文件中设置 Zend Framework 库的包含路径。

自动加载器类加载是因为您将require_once 添加到具有自动加载器类定义的确切文件中。据我了解BLOCKS_ROOT_ENGINE 常量在您的index.php 中定义。

另外我猜你添加了 /opt/local/apache2/htdocs/blocks 路径以包含 index.php 中的路径 - 那是因为自动加载器能够加载 Engine_Zend_Config_Reader_Xml 类。

必须在您的index.php 中添加BLOCKS_ROOT_ENGINE 以包含路径列表以允许自动加载器找到Zend\Config\Reader\ReaderInterface

【讨论】:

  • BLOCKS_ROOT_ENGINE 在 index.php 中
  • 我还必须设置哪些其他包含路径才能让这个东西正常工作
  • @numerical25 请将您的index.php 的内容添加到您的帖子中
【解决方案2】:

不确定这是否是您的情况,但有一次我在允许的情况下与我的学生遇到了奇怪的问题。 一名学生(具有 root 权限)使用文件夹权限做了一些事情,我们无法找出是什么 - 但该文件夹中的文件无法再被 php 访问。

甚至 file_exists() 都返回 false。 我们最终通过复制整个文件夹,删除原始文件夹并使用原始名称重命名复制的文件夹来解决问题。

请检查您的权限是否正常。

【讨论】:

    【解决方案3】:

    这个问题与我设置了自动加载器有关。我下载了skeleton ZF2,发现这个文件对设置很有帮助

    <?php
    /**
     * Zend Framework (http://framework.zend.com/)
     *
     * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
     * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
     * @license   http://framework.zend.com/license/new-bsd New BSD License
     */
    
    /**
     * This autoloading setup is really more complicated than it needs to be for most
     * applications. The added complexity is simply to reduce the time it takes for
     * new developers to be productive with a fresh skeleton. It allows autoloading
     * to be correctly configured, regardless of the installation method and keeps
     * the use of composer completely optional. This setup should work fine for
     * most users, however, feel free to configure autoloading however you'd like.
     */
    
    // Composer autoloading
    if (file_exists('vendor/autoload.php')) {
        $loader = include 'vendor/autoload.php';
    }
    
    $zf2Path = false;
    
    if (is_dir('vendor/ZF2/library')) {
        $zf2Path = 'vendor/ZF2/library';
    } elseif (getenv('ZF2_PATH')) {      // Support for ZF2_PATH environment variable or git submodule
        $zf2Path = getenv('ZF2_PATH');
    } elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value
        $zf2Path = get_cfg_var('zf2_path');
    }
    
    if ($zf2Path) {
        if (isset($loader)) {
            $loader->add('Zend', $zf2Path);
        } else {
            include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
            Zend\Loader\AutoloaderFactory::factory(array(
                'Zend\Loader\StandardAutoloader' => array(
                    'autoregister_zf' => true
                )
            ));
        }
    }
    
    if (!class_exists('Zend\Loader\AutoloaderFactory')) {
        throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 2013-05-08
      • 1970-01-01
      相关资源
      最近更新 更多