Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2009-08-15 05:42:58

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Class Not Found!

I've noticed this problem happens a lot when you're just starting out with the Zend Framework. Oftentimes you'll be following a tutorial and it comes with a need little class that look like:
My_forms_MyDate
ZC_MyCustomClass
App_BaseController

etc...

basically, in the "My_forms_" example, the class is sitting in the "My" folder in the library/ folder:

/library/My/forms/MyDate.php

in order for this class to be autoloaded by the zend framework, you simply need to register the namespace. In your Bootstrap.php file, you'll need something like this:

[code=php]
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH));

        $autoloader = Zend_Loader_Autoloader::getInstance();
//registering the ZC_* namespace and the My_* namespace
        $autoloader->registerNamespace(array('ZC_','My_'));
        return $moduleLoader;       
    }
}
[/code]
As a freelancer, I've built up my own custom library of Zend components that have saved me hours of re-typing!

Offline

#2 2009-09-16 19:11:10

mikemmx
New member
From: USA, massachusetts
Registered: 2009-09-11
Posts: 7
Website

Re: Class Not Found!

I ran into this problem when following your video tutorials. Didn't know you had a forum so i googled an got this which i prefer better because you don't have to register individual namespace.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload() {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
        'namespace' => '',
        'basePath' => APPLICATION_PATH));
        
        /** auto load */
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->setFallbackAutoloader(true);
        return $moduleLoader;
    }
}

Offline

#3 2009-09-16 19:15:43

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: Class Not Found!

ooh! I like this! Thanks for posting it mikemmx

Offline

#4 2009-09-29 19:01:36

James S
New member
Registered: 2009-09-02
Posts: 1

Re: Class Not Found!

On the subject of autoloading, I've recently taken to adding my forms and ACLs to the autoloader under App_Form_* and App_Acl_*

protected function _initAutoload() {
    require_once('Zend/Loader.php');
    $loader = Zend_Loader_Autoloader::getInstance();
    $loader->pushAutoloader(new Zend_Loader_Autoloader_Resource(array(
        'basePath' => APPLICATION_PATH,
        'namespace' => 'App',
        'resourceTypes' => array(
            'acl' => array(
                'path'      => 'acls/',
                'namespace' => 'Acl'
            ),
            'form' => array(
                'path'      => 'forms/',
                'namespace' => 'Form'
            ),
            'model' => array(
                'path'      => 'models/',
                'namespace' => 'Model'
            )
        )
    )));
    $loader->registerNamespace('Project');
    return $loader;
}

This also adds the advantage of loading /application/models as App_Model_*

Like it so far, thought I'd share. smile

Offline

#5 2010-02-28 18:30:23

muasif80
New member
Registered: 2010-02-28
Posts: 2

Re: Class Not Found!

Dear Jon,

I have done almost everything to autoload my acls file using the above exact code. But I am failed to autoload the acls file it is in the directory

application/acls/visitor.php

and its class is

Application_Acl_Visitor

But whenever I try to instantiate this class in my index controller, It gives error that 'class not found'

I have the exact above method copied in my bootstrap and i have put the namespace as Application_

If you can please tell what could be the possible error I will be very very thankful to you.

I have googled a lot and have consulted the reference document but to no avail.

Regards,
Asif

Offline

#6 2010-02-28 18:31:52

muasif80
New member
Registered: 2010-02-28
Posts: 2

Re: Class Not Found!

My framework version is 1.10

Offline

#7 2010-09-04 02:29:32

winechristmas
New member
From: USA
Registered: 2010-09-04
Posts: 1

Re: Class Not Found!

Zend_Loader_Autoloader introduces a comprehensive autoloading solution for Zend Framework. It has been designed with several goals in mind:

    *

      Provide a true namespace autoloader. (Previous incarnations intercepted all userland namespaces.)
    *

      Allow registering arbitrary callbacks as autoloaders, and manage them as a stack. (At the time of this writing, this overcomes some issues with spl_autoload, which does not allow re-registering a callback that utilizes an instance method.)
    *

      Allow optimistic matching of namespaces to provide faster class resolution.

Zend_Loader_Autoloader implements a singleton, making it unversally accessible. This provides the ability to register additional autoloaders from anywhere in your code as necessary.
The first time an instance of the autoloader is retrieved, it registers itself with spl_autoload. You retrieve an instance using the getInstance() method:

   1.
      $autoloader = Zend_Loader_Autoloader::getInstance();

By default, the autoloader is configured to match the "Zend_" and "ZendX_" namespaces. If you have your own library code that uses your own namespace, you may register it with the autoloader using the registerNamespace() method. For instance, if your library code is prefixed with "My_", you could do so as follows:

   1.
      $autoloader->registerNamespace('My_');

    Note: Namespace Prefixes
    You'll note that the previous example uses "My_" and not "My". This is because Zend_Loader_Autoloader is intended as a general purpose autoloader, and does not make the assumption that a given class prefix namespace includes an underscore. If your class namespace does include one, you should include it when registering your namespace.

You can also register arbitrary autoloader callbacks, optionally with a specific namespace (or group of namespaces). Zend_Loader_Autoloader will attempt to match these first before using its internal autoloading mechanism.

As an example, you may want to utilize one or more eZcomponents components with your Zend Framework application. To use its autoloading capabilities, push it onto the autoloader stack using pushAutoloader():

   1.
      $autoloader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');

This tells the autoloader to use the eZcomponents autoloader for classes beginning with "ezc".

You can use the unshiftAutoloader() method to add the autoloader to the beginning of the autoloader chain.

By default, Zend_Loader_Autoloader does no error suppression when using its internal autoloader, which utilizes Zend_Loader::loadClass(). Most of the time, this is exactly what you want. However, there may be cases where you want to suppress them. You can do this using suppressNotFoundWarnings():

   1.
      $autoloader->suppressNotFoundWarnings(true);

Finally, there may be times when you want the autoloader to load any namespace. For instance, PEAR libraries do not share a common namespace, making specifying individual namespaces difficult when many PEAR components are in use. You can use the setFallbackAutoloader() method to have the autoloader act as a catch-all:

   1.
      $autoloader->setFallbackAutoloader(true);

    Note: Loading Classes from PHP Namespaces
    Starting in version 1.10.0, Zend Framework now allows loading classes from PHP namespaces. This support follows the same guidelines and implementation as that found in the » PHP Framework Interop Group PSR-0 reference implementation.
    Under this guideline, the following rules apply:

        *

          Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.
        *

          Each "_" character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The "_" character has no special meaning in the namespace.
        *

          The fully-qualified namespace and class is suffixed with ".php" when loading from the file system.

    As examples:

        *

          \Doctrine\Common\IsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
        *

          \namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php
        *

          \namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php

for more visit http://framework.zend.com/manual/en/zen … oader.html

Offline

#8 2010-10-24 13:18:27

petrovyoung
New member
Registered: 2010-10-24
Posts: 2

Re: Class Not Found!

mikemmx wrote:

I ran into this problem when following your video tutorials. Didn't know you had a forum so i googled an got this which i prefer better because you don't have to register individual namespace.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload() {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
        'namespace' => '',
        'basePath' => APPLICATION_PATH));
        
        /** auto load */
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->setFallbackAutoloader(true);
        return $moduleLoader;
    }
}

Thanks mike for this post. The code is proved useful to me. I am using it now

Offline

#9 2010-10-24 13:22:01

petrovyoung
New member
Registered: 2010-10-24
Posts: 2

Re: Class Not Found!

mikemmx wrote:

I ran into this problem when following your video tutorials. Didn't know you had a forum so i googled an got this which i prefer better because you don't have to register individual namespace.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload() {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
        'namespace' => '',
        'basePath' => APPLICATION_PATH));
        
        /** auto load */
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->setFallbackAutoloader(true);
        return $moduleLoader;
    }
}

Thanks mike for this post. The code is proved useful to me. I am using it now

Offline

#10 2010-12-10 09:38:50

Latios88
New member
Registered: 2010-12-10
Posts: 2

Re: Class Not Found!

It´s possible to configure the resource autoloader in the application.ini?

Offline

Board footer

Powered by FluxBB