Page 1 of 1

Zend framework naming conventions?

Posted: Wed Jan 31, 2007 5:07 pm
by xinnex
Everybody seems to be looking into the Zend framework for obvious reasons, but one thing strikes me as.. well.. a bit odd.

Assuming I have a folder layout like the following:

Code: Select all

/application
  /MyApp
    /model
    /view
    /controller
/document_root
  /images
  /styles
  .htaccess
  index.php
/library
  /Zend
Then my logic would be to name my indexcontroller pear-style (like the rest of the framework?):

Code: Select all

class MyApp_Controller_IndexController extends Zend_Controller_Action
{
	//...
}
Instead the manual currently advocates the following naming convention:

Code: Select all

class IndexController extends Zend_Controller_Action
{
	//...
}
In short: Why isn't the pear naming style advocated when writing your own applications? Wouldn't advocating the same style make it possible to reuse more code?
Like if I wanted to reuse the "MyApp"-folder as a library in another application?

PS. I'm aware that the framework manual is frequently updated, but it still seemed worth discussing.

Posted: Wed Jan 31, 2007 5:41 pm
by Maugrim_The_Reaper
As a naming convention, the ZF doesn't force you to a single path. Largely though having simpler naming means the router logic (for mapping requests to a controller and action method) is simpler. For all other classes which are not controllers, and are aimed for reuse, then feel free to use the PEAR convention. If you want to use different controller names, you could just subclass the RewriteRouter or Router class and override to add such functionality - one of the pluses of the ZF is it doesn't force you to a path and there plenty of scope for subclassing.

Posted: Wed Jan 31, 2007 5:47 pm
by Christopher
The concept is that class names are the same as the relative path from some predefined base path. So the 'Zend' directory and any other support libraries are in the include path. Action Controllers, however, are not loaded manually using include() but are loaded by the Front Controller from the base path given it. And in turn Models and Views are loaded by the Action Controller. So the two naming schemes are due to two different loading schemes.

Posted: Wed Jan 31, 2007 5:56 pm
by xinnex
Maugrim_The_Reaper wrote:As a naming convention, the ZF doesn't force you to a single path.
That's true..
Maugrim_The_Reaper wrote:If you want to use different controller names, you could just subclass the RewriteRouter or Router class and override to add such functionality
Yeah, I was planning to do just that in my application. Nevertheless I still find it odd not to use a standard when one of the visions with the ZF, was to help developers standardize?

I'm one lazy programmer and I would love to reuse more code from other PHP-projects, but the lack of consistent naming has given me many a headache so far. If the ZF could help in this area, I'd be one happy dolphin.

Posted: Wed Jan 31, 2007 6:02 pm
by RobertGonzalez
Moved from General Discussion to PHP - Theory and Design

Posted: Wed Jan 31, 2007 6:05 pm
by xinnex
Everah wrote:Moved from General Discussion to PHP - Theory and Design
Sorry about that =)

Posted: Wed Jan 31, 2007 6:10 pm
by xinnex
arborint wrote:The concept is that class names are the same as the relative path from some predefined base path. So the 'Zend' directory and any other support libraries are in the include path. Action Controllers, however, are not loaded manually using include() but are loaded by the Front Controller from the base path given it. And in turn Models and Views are loaded by the Action Controller. So the two naming schemes are due to two different loading schemes.
So I'm actually missing the point totally.. If I want to design my code for reuse, I should rather place such in a library and include it from the application?

Still, how would you reuse fx. views if I went along with the current ZF implementation (also a question to you Maugrim, since your point is still valid here)?

Posted: Wed Jan 31, 2007 6:32 pm
by Christopher
xinnex wrote:So I'm actually missing the point totally.. If I want to design my code for reuse, I should rather place such in a library and include it from the application?
Well you should be able to reuse Models and Views in different Controllers given that you are using a non-modular layout.
xinnex wrote:Still, how would you reuse fx. views if I went along with the current ZF implementation (also a question to you Maugrim, since your point is still valid here)?
If you have library classes then put them in you own hierarchy parallel to the Zend directory.

Posted: Wed Jan 31, 2007 7:55 pm
by Ollie Saunders
I've experienced this exact same problem and been irritated by it until I realized that I never actually need to load controllers unless they are abstract or something. Controller inheritance probably isn't such a great idea anyway.

The thing that annoys me about conventions is working with multiple libraries with different conventions. SimpleTest is one of the worst offenders for class names. I think everybody should take ZF's lead and start using _ to / substitution. It's a great way to work. Ideally you want an environment where

Code: Select all

function __autoload($class)
{
    Zend::loadClass($class);
}
will load all the libraries you need as well as your own code.

Posted: Thu Feb 01, 2007 3:08 am
by Maugrim_The_Reaper
Still, how would you reuse fx. views if I went along with the current ZF implementation (also a question to you Maugrim, since your point is still valid here)?
If you mean the base View templates?

Well, they don't contain any class information except for adherence to the interface (Zend_View stores variables as public class properties). If you were switching to another framework or template engine, you'd need to fence the new Template Engine behind an adapter (see "Adapter Pattern" on google) so that you have a layer which looks and acts like Zend_View but which proxies to the template engine in use. Mainly that would mean having magic __get and __set methods to assign and fetch template variables.

Zend_View is not really very complex (ignore the helper/script/funny stuff and it's actually very simple) so it's easy to use it as a dumb adapter across a larger template engine like Smarty.

Many frameworks should have some way of fencing its template engine....well, hopefully. Any PhpCake fans with some advice?