问题描述
我需要对Zend Framework 2.0 中的自定义类使用自动加载功能.我的自定义库位于/vendor/Garvey/library/Garvey
.我在/vendor/Garvey/library/Garvey/Db/Table/AbstractTable.php
中有一个简单的扩展AbstractTable类:
I need to use autoloading for my custom classes in Zend Framework 2.0. My custom library located in /vendor/Garvey/library/Garvey
. I have a simple extended AbstractTable class in /vendor/Garvey/library/Garvey/Db/Table/AbstractTable.php
:
<?php
namespace Garvey\Db\Table;
use Zend\Db\Table\AbstractTable;
abstract class AbstractTable extends AbstractTable
{
public function getItemById($id)
{
}
}
在index.php中,我有以下代码:
In the index.php I have the following code:
require_once 'vendor/ZendFramework/library/Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory(array('Zend\Loader\StandardAutoloader' => array(
'prefixes' => array(
'Garvey' => 'vendor/Garvey/library/Garvey',
)
)));
但是我有以下错误.我错过了什么?
But I have the following error. What I have missed?
Fatal error: Class 'Garvey\Db\Table\AbstractTable' not found
谢谢.
推荐答案
如果您将'prefixes'键更改为'namespaces'并指定如下路径,您的原始index.php也将起作用:
Your original index.php would also worked if you changed the 'prefixes' key to 'namespaces' and specify path like below:
Zend\Loader\AutoloaderFactory::factory(array('Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
'Garvey' => dirname(__DIR__) . '/vendor/Garvey',
)
)));
这篇关于在Zend Framework 2.0中自动加载自定义库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!