zend-frameworkzend-autoloader

zend won't recognize registered namespace


I'm using the Zend Standard Autoloader. 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:

Model.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

<?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

Which is where this line is in my Coop Model:

class Coop extends Model

Thanks for any help!


Solution

  • The ZF2 standard autoloader is a PSR-0 compliant autoloader. 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).

    Also, if at all possible I'd recommend using Composer to install ZF2 (or whichever components you're using). Then you wouldn't need to configure the autoloader yourself at all.