Link to home
Start Free TrialLog in
Avatar of davidsperling
davidsperling

asked on

Zend Framework - Zend_Loader_Autoloader doesen't work for me

Can't seem to get autoload working.

See screenshots!




*** Bootstrap.php ***
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{


    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    protected function _initAutoloader()
    {
        $loader=Zend_Loader_Autoloader::getInstance();
        $loader->setFallbackAutoloader(true);
    }

}




*** TestController.php ***
<?php

class TestController extends Zend_Controller_Action
{
    public function indexAction()
    {

        $b=get_include_path(); //debug - yes
        $a=new mylittlekalle("littlekalle");
        
    }

}


*** kallbanan.php ***
<?php
class mylittlekalle extends Zend_Form_Element_Select
{
    public function kalle()
    {
        return "kalle";
    }
}


*** index.php ***
<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));



// Ensure /application is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH ),
    get_include_path(),
)));


// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
            ->run();

Open in new window

dirs.png
error.png
Avatar of midhungirish
midhungirish
Flag of Heard Island and McDonald Islands image

Well for autoloding to work, you need to specify the namespace..... You can register a namespace like 'App' and use it as the prefix of the resources..... Also the default autloader settings checks for forms in the "application/forms" directory.. if you want to change it, you need to specify it....  Im not sure the following code works coz i dont have the setup to test it.. Jsut check it out.. may be it will ring a bell....
*** Bootstrap.php ***
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{


    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    protected function _initAutoloader()
    {
        $loader=Zend_Loader_Autoloader::getInstance();
		$loader->registerNamespace('App_');
        $loader->setFallbackAutoloader(true);
    }

}




*** TestController.php ***
<?php

class TestController extends Zend_Controller_Action
{
    public function indexAction()
    {

        $b=get_include_path(); //debug - yes
        $a=new App_Form_mylittlekalle("littlekalle");
        
    }

}


*** forms/mylittlekalle.php ***
<?php
class App_Form_mylittlekalle extends Zend_Form_Element_Select
{
    public function kalle()
    {
        return "kalle";
    }
}


*** index.php ***
<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));



// Ensure /application is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH ),
    get_include_path(),
)));


// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
            ->run();

Open in new window

Avatar of davidsperling
davidsperling

ASKER

"$loader->setFallbackAutoloader(true)" means that you don't have to register namespaces, doesn't it?

I've also tried with namespaces, but it still doesn't work. Have no idea why.
... and since /application is in inlude path, classes in /application/kalle should be detected automatically, shouldn't them?
ASKER CERTIFIED SOLUTION
Avatar of midhungirish
midhungirish
Flag of Heard Island and McDonald Islands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have absolutely no luck at all!

Let's put it this way...

Can you modify the ZF Quick Start project so that  Zend_Loader_Autoloader is used on a custom folder named myclasses (residing in application path) with namespace Mynamespace_?

http://framework.zend.com/demos/ZendFrameworkQuickstart.zip
http://framework.zend.com/demos/ZendFrameworkQuickstart.tar.gz

I don't want to mess around with a lot of "require_once"... That's not why I use ZF!
Sorry, you were right all along ;)

ee.png