Solved

Zend Framework - Zend_Loader_Autoloader doesen't work for me

Posted on 2010-09-02
7
793 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
Industry Leaders: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 9 53
scan php uploads for viruses 5 23
HTTPS for signup & login - HTTP for all other pages 10 43
Special characters in a TCPDF 4 23
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

735 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