Solved

Zend Framework - Zend_Loader_Autoloader doesen't work for me

Posted on 2010-09-02
7
795 Views
Last Modified: 2013-11-10
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
0
Comment
Question by:davidsperling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 6

Expert Comment

by:midhungirish
ID: 33585231
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

0
 
LVL 4

Author Comment

by:davidsperling
ID: 33585301
"$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.
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33585328
... and since /application is in inlude path, classes in /application/kalle should be detected automatically, shouldn't them?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
midhungirish earned 500 total points
ID: 33585676
As a matter of fact $loader->setFallbackAutoloader(true) doesnt mean you dont have tio use name spaces but it means that any name space will be autoloaded.. ie you done need to register it explicitly...... For your code to work, use the name "kalle_mylittlekalle" for the class...... ie 'kalle_' will be the namespace....... so the script looks for kalle/mylittlekalle.php".... Hope you got it...
**kalle/mylittlekalle.php**

<?php
class kalle_mylittlekalle extends Zend_Form_Element_Select
{
    public function kalle()
    {
        return "mylittlekalle new";
    }
}

**TestController.php**
<?php

class TestController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $b=get_include_path(); //debug - yes
        $a=new kalle_mylittlekalle("littlekalle");

    }


}

Open in new window

0
 
LVL 4

Author Comment

by:davidsperling
ID: 33588004
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!
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33588162
Sorry, you were right all along ;)

ee.png
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33588225
..
ee2.png
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question