Read the latest web development and design tips at Fred Wu's new blog! :-)
Poke me on GitHub

Class naming convention in Zend Framework makes life harder

First of all, not all parts of the Zend Framework enforce the class naming convention, at least not upon calling ‘em. For example, view helpers are called directly by the ‘chopped’ class name:

// actual class name:
class Path_To_The_CustomViewHelper {}

// calling it in the view:
$this->CustomViewhelper();

Also, models are both named, and called by their class names without any paths (but yes we have to include them in the include paths).

You would think that in most cases calling the classes by their full names (with paths) should work, right?

Wrong.

The class naming convention gets extremely confusing when used with modules.

In modules, the framework actually expects class names without the ‘controllers’ part: so if your controller is placed in ‘Mymodule/Controllers/Mycontroller.php’, the correct class name should be ‘Mymodule_Mycontroller’ (the controller name prefixed by the module name). This is fine if we don’t need to cross-reference controllers between modules, but if we do, we’d get a nasty exception (and Zend Framework’s exception / stack trace page is really fugly). This is because the framework expects the class name with full path this time. Sounds confusing yeah?

In a nutshell, the framework is expecting ‘Mymodule_Mycontroller’ from within the module, but is expecting ‘Mymodule_Controllers_Mycontroller’ from outside of the module.

A quick fix would be:

Name the class ‘Mymodule_Mycontroller’, and after the class make an empty dummy class:

class Mymodule_Controllers_Mycontroller extends Mymodule_Mycontroller {}

Oh and if the classes are in the default module, you can NOT prefix them with the module name.

Now Zend Framework is happy.

I am not sure if I have missed anything here since the documentation really is not ideal for finding out specific information. Perhaps someone could shed some lights?

  • Digg
  • DZone
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

Related posts

Tags: , ,

Comments Section

4 Responses to “Class naming convention in Zend Framework makes life harder”

Sidebar might be covered by comments ... consider it a feature! ;)
  1. 1

    Zend_Loader always expects class names to match paths, this is how it knows where to find them. Helpers and Controller classes are special cases, are used internally and shouldn’t ever need to be loaded by Zend_Loader, which is why they have different naming conventions.

    The only situation I can think of in which this would be an issue would be if you were trying to get one of your controllers to extend some base class that wasn’t yet loaded. Zend_Loader would then try and find the class based on the name. Is this what you’re trying to do?

  2. 2

    I agree that the naming conventions/requirements can be somewhat confusing, but it does seem like a good balance between representing the MVC architecture in the default directory structures and having a standard naming convention.

  3. 3

    It’s really rare to need to load a controller manually as any common functionality between controllers should be factored out in action helpers.

    Regards,

    Rob…

  4. 4

    I have to agree with the fact that it makes life harder…
    they don’t follow their own convention!
    I have tried to have a simple solution to have a controller inheriting from another one (e.g. testController extends indexController )… it can be done, but it cannot be loaded just by the autoloader … so it needs a stupid fix!
    too bad ;-)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>