+ Reply to Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 12 of 12

Thread: Autoloading Models

  1. #11
    gauthier is offline Junior Member
    Join Date
    Sep 2008
    Posts
    1

    Lightbulb another proposal for model autoloading

    hi guys,

    here is the way I handle this issue :

    1) add the models foldes to include path :

    [PHP]// add ZF Home and models folders to include_path
    $includePath = array(
    '/opt/ZendFramework/1.6/library/' ,
    '../library' ,
    '../application/common/models/' ,
    '../application/forum/models/' ,
    '../application/admin/models/' ,
    '.' ,
    get_include_path()
    );
    set_include_path(implode(PATH_SEPARATOR, $includePath));[/PHP]

    2) register an autoload function

    a] under windows
    [PHP]spl_autoload_register();[/PHP]
    By doing this, spl_autoload() will be automatically registered as autoloader, and will look for {$classname}.php and {$classname}.inc in every directory listed in include path.

    b] under GNU/Linux
    You may paid attention to the {$classname} instead of {$className}? If yes, you already understood why it is necessary to create a home made autoloader function for Linux : spl_autoload() lowercase the class name when it looks for the source file. So, when there is no problem with Windows (whose filesystem is case insensitive), using this trick under Linux would lead you to break the file naming convention of ZF. To avoid this horrible crime , just add this code somewhere in/from your bootstrap :

    [PHP]function modelAutoloader($className) {
    require_once "$className.php";
    }
    spl_autoload_register('modelAutoLoader');
    [/PHP]
    Last edited by gauthier; 09-05-2008 at 03:30 PM.

  2. #12
    farazdagi is offline Junior Member
    Join Date
    Mar 2009
    Posts
    1

    Default

    Yet another way would be to extend your action controller, so that it has method for locating and loading the module (as generally models are needed within actions). Here is small article on how to do this: ZF: models auto-loading.

+ Reply to Thread
Page 2 of 2 FirstFirst 1 2

Similar Threads

  1. Modules, models and autoloading...
    By ajlisowski in forum General Q&A on Zend Framework
    Replies: 3
    Last Post: 07-11-2010, 09:17 AM
  2. autoloading default modules models
    By saif in forum General Q&A on Zend Framework
    Replies: 0
    Last Post: 03-02-2010, 11:04 AM
  3. autoloading
    By chinaski in forum Core Infrastructure
    Replies: 3
    Last Post: 11-21-2009, 03:12 PM
  4. Autoloading models?
    By ArticSun in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 11-16-2009, 07:41 AM
  5. Autoloading Models and DB_DataObjects
    By CreedFeed in forum Installation & Configuration
    Replies: 0
    Last Post: 06-10-2009, 12:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts