Zend framework naming conventions?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Zend framework naming conventions?

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Moved from General Discussion to PHP - Theory and Design
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Post by xinnex »

Everah wrote:Moved from General Discussion to PHP - Theory and Design
Sorry about that =)
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Post 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)?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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?
Post Reply