在 Zend Autoloader 中找不到 PHPExcel 类 [英] PHPExcel class not found in Zend Autoloader

查看:43
本文介绍了在 Zend Autoloader 中找不到 PHPExcel 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Zend Framework 中为命名空间而苦苦挣扎(至少我认为这是一个命名空间问题).

我想将 PHPExcel 集成到我的 Zend 项目中.相关文件结构如下:

<预><代码>/-图书馆-A B C D-PHPExcel-Zend-ZendX-PHPExcel.php

自定义类工作正常,之后

Zend_Loader_Autoloader::getInstance()->registerNamespace('ABCD_');

在引导程序中.此外,这些类都被命名为 ABCD_blahdeblah.

然而,执行 registerNamespace('PHPExcel_') 并不能帮助 Zend 找到合适的类.当我尝试

$sheet = new PHPExcel;

在控制器中,我收到找不到类"错误.我猜这是因为 PHPExcel 中的类没有使用命名空间前缀命名,或者因为主要的 PHPExcel.php 文件位于我刚刚声明的命名空间之外.但是 PHPExcel 结构要求它位于其余 class/font/etc 文件的父目录中.

任何指针将不胜感激.

提前致谢.

解决方案

为 PHPExcel 创建自动加载器并将其添加到 Zend 自动加载器堆栈.

library/My/Loader/Autoloader/PHPExcel.php中:

class My_Loader_Autoloader_PHPExcel 实现 Zend_Loader_Autoloader_Interface{公共函数自动加载($class){if ('PHPExcel'!= $class){返回假;}require_once 'PHPExcel.php';返回 $class;}}

application/configs/application.ini中:

autoloadernamespaces[] = "My_"

然后,在application/Bootstrap.php:

受保护的函数 _initAutoloading(){$autoloader = Zend_Loader_Autoloader::getInstance();$autoloader->pushAutoloader(new My_Loader_Autoloader_PHPExcel());}

然后您应该能够实例化 PHPExcel - 例如,在控制器中 - 使用一个简单的:

$excel = new PHPExcel();

唯一棘手的部分是 PHPExcel 如何处理在其自己的文件夹中加载所有依赖项.如果这是智能完成的 - 或者使用像 require_once basename(__FILE__) 这样的调用.'/someFile.php' 或它自己的自动加载器,不知何故不会妨碍 Zend 自动加载器 - 那么一切都应该很酷.#着名的lastwords

I am struggling with namespaces in Zend Framework (at least I think it's a namespace issue).

I want to integrate PHPExcel into my Zend project. Relevant file structure is as follows:

/
 -library
   -ABCD
   -PHPExcel
   -Zend
   -ZendX
   -PHPExcel.php

Custom classes work fine, after

Zend_Loader_Autoloader::getInstance()->registerNamespace('ABCD_');

in the bootstrap. Also, those classes are all named ABCD_blahdeblah.

However, doing registerNamespace('PHPExcel_') doesn't help Zend find the appropriate classes. When I try

$sheet = new PHPExcel; 

in the controller, I get a "Class not found" error. I am guessing that this is either because classes in PHPExcel aren't named with the namespace prefix, or because the main PHPExcel.php file sits outside of the namespace I've just declared. But the PHPExcel structure demands that it sit in the parent directory of the rest of the class/font/etc files.

Any pointers would be greatly appreciated.

Thanks in advance.

解决方案

Create an autoloader for PHPExcel and add it to the Zend autoloader stack.

In library/My/Loader/Autoloader/PHPExcel.php:

class My_Loader_Autoloader_PHPExcel implements Zend_Loader_Autoloader_Interface
{
    public function autoload($class)
    {
        if ('PHPExcel' != $class){
            return false;
        }
        require_once 'PHPExcel.php';
        return $class;
    }
}

And in application/configs/application.ini:

autoloadernamespaces[] = "My_"

Then, in application/Bootstrap.php:

protected function _initAutoloading()
{
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->pushAutoloader(new My_Loader_Autoloader_PHPExcel());
}

Then you should be able to instantiate PHPExcel - say, in a controller - with a simple:

$excel = new PHPExcel();

The only sticky part is all of this is how PHPExcel handles loading all its dependencies within its own folder. If that is done intelligently - either with calls like require_once basename(__FILE__) . '/someFile.php' or with its own autoloader that somehow doesn't get in the way of the Zend autoloader - then all should be cool. #famouslastwords

这篇关于在 Zend Autoloader 中找不到 PHPExcel 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆