简体   繁体   English

zend无法识别注册的名称空间

[英]zend won't recognize registered namespace

I'm using the Zend Standard Autoloader. 我正在使用Zend Standard自动装带器。 It's registering one namespace, but it won't register the other. 它正在注册一个名称空间,但不会注册另一个名称空间。 This is my code: 这是我的代码:

$zflib = $_SERVER['SERVER_ROOT'].'/classes/Zend_Framework_2/2.3.2/library';
require_once($zflib.'/Zend/Loader/StandardAutoloader.php');
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
// Library
$loader->registerNamespace('VProd',$_SERVER['SERVER_ROOT'].'/classes/VProd');
// Dealer Library
$loader->registerNamespace('Dealers', $_SERVER['SERVER_ROOT'].'/dealers/classes');
$loader->setFallbackAutoloader(true);
$loader->register();

I then have basic setup like this in the dealers/classes directory: 然后,我在Dealers / classes目录中进行了如下基本设置:

Model.php 模型.php

<?php

namespace Dealers\Models;

class Model {
    /**
     * The table this model uses
     *
     * @var string
     */
    protected $table;
}

Coop Model: 鸡舍模型:

<?php

namespace Dealers\Models\Coop;

use Dealers\Models\Model;

class Coop extends Model {
    /**
     * The table this model uses
     *
     * @var string
     */
    protected $table = 'coop';

    public static function testing()
    {
        return 'testing';
    }
}

In my application I'm including that config file BEFORE anything else. 在我的应用程序中,我将在其他任何内容之前包含该配置文件。

coop.php coop.php

<?php
require_once($_SERVER['SERVER_ROOT'].'/security/config.php');

use Dealers\Model\Coop;
echo CoopBalance::testing();

I'm getting this error message: 我收到此错误消息:

Fatal error: Class 'Dealers\\Models\\Model' not found in \\www\\dealers\\classes\\coop\\Coop.php on line 7 致命错误:在第7行的\\ www \\ dealers \\ classes \\ coop \\ Coop.php中找不到类'Dealers \\ Models \\ Model'

Which is where this line is in my Coop Model: 这行在我的Coop模型中的位置:

class Coop extends Model

Thanks for any help! 谢谢你的帮助!

The ZF2 standard autoloader is a PSR-0 compliant autoloader. ZF2标准自动装带器是符合PSR-0的自动装带器。 So your classes should be at dealers/classes/Dealers/Models/Model.php and dealers/classes/Dealers/Models/Coop/Coop.php (case sensitive) to get autoloaded properly (each 'part' of the namespace should be a folder). 因此,您的班级应位于dealers/classes/Dealers/Models/Model.phpdealers/classes/Dealers/Models/Coop/Coop.php (区分大小写)上,以正确正确地自动加载(名称空间的每个“部分”应为夹)。

Also, if at all possible I'd recommend using Composer to install ZF2 (or whichever components you're using). 另外,如果可能的话,我建议您使用Composer安装ZF2(或您使用的任何组件)。 Then you wouldn't need to configure the autoloader yourself at all. 然后,您根本不需要自己配置自动加载器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM