Zend Framework

Autoloading Zend Framework Like Classes In Kohana v3

I admit, I’m a Zend Framework addict! So, I want to code using Zend Framework coding/naming conventions even in Kohana v3. However, I recently found out that it is too easy to autoload your ZF like classes in Kohana v3 with a simple tweak on Kohana core.

To explain further, I will give you an example of how the Zend Framework class and file naming convention is. For example you have a class name:

My_Log_File_Writer

in Zend Framework, it is mapped into My/Log/File/Writer.php and it doesn’t matter where the My directory is located as long as it is added to PHP’s include path. However, in Kohana v3, file names are converted to lowercase. So the class My_Log_File_Writer will be saved under:

application/classes/my/log/file/writer.php

If you want it to be in application directory. You can also put it on the system directory of even modules directory when applicable.

Aside from the file naming convention, I also like the function/variable naming of Zend Framework. So to allow me to use my preferred naming, I will tweak the Kohana core autoloading function in a clean and maintainable way.

The root of all evil is the file system/classes/kohana/core.php which has the method auto_load(). I will change that to my own, but still be able to load kohana related classes and not actually modifying Kohana v3 framework’s core files.

Good thing is that most of the core classes in Kohana v3 is one class away from what is used in every application. What I mean is that before a Kohana core file is used, there is an intermediate class which we can call as the general class which extends the Kohana core class.

For example, we use the class Controller to write our own controllers like this:

class Controller_User extends Controller{}

But actually, Controller class extends Kohana_Controller which does the real controller thing. And in Kohana v3, there are 3 places where a class can be found: in application, modules and system path, where application path is the top priority and when a class is first found in application path, it will be the one used.

So finally, we come up with the solution – overrride the Kohana_Core class. Kohana is the general class name which extends Kohana_Core. We will create a new file named kohana, lowercase and put it inside the application/classes path. This is going to be the content.

auto_load() function. First, it will look for the file using lowercase filename. If it cannot find it, it will find the file without converting the filename to lowercase. As a result, we are now free to use the Zend Framework file naming conventions but just be sure to put it on the right directory. Preferably in the application directory.

2 thoughts on “Autoloading Zend Framework Like Classes In Kohana v3”

  1. The Kohana::find_file() function is not case sensitive so it is not needed to split the file path like that. What is important, however, is the way Zend includes his own library files.

    Things like require_once 'Zend/Locale.php'; that some files require won’t work in Kohana if you don’t set the include path right.

    So my suggestion for extending the auto load function would rather be:


    public static function auto_load($class)
    {
    // Transform the class name into a path
    $file = str_replace('_', '/', strtolower($class));

    if(stripos($file, 'Zend') !== false)
    {
    set_include_path(MODPATH.'zend/classes/');
    }
    else
    {
    restore_include_path();
    }

    if ($path = Kohana::find_file('classes', $file))
    {
    // Load the class file
    require $path;

    // Class has been found
    return TRUE;
    }

    // Class is not in the filesystem
    return FALSE;
    }

    (Providing you put the entire Zend library in modules/zend/classes, like I did)

  2. Hi Melvin Tercan,

    Although I now kinda hate Zend Framework a little, but in this old post I stripped off all require_once calls in a certain ZF component which was Zend_Acl. So no need to use include path setting.

    I also enhanced the above post with this post: http://blog.lysender.com/2010/03/autoloading-zend-framework-like-classes-in-kohana-v3-enhanced/

    By the way, I like the idea of checking Zend from the class name although I used my own ZF compatible library sometimes.

    Thank you for dropping by.

Leave a reply

Your email address will not be published. Required fields are marked *