Zend framework coding standards

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

matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Zend framework coding standards

Post by matthijs »

Before you reply angry that the topic of coding standards has been here too often, let me explain my question. I'm a bit confused as to what standards I should follow for my own code base, if I decide to follow zend's. I've read the manual and have looked through a few code bases, but still confused.

According to the manual the files should follow:

Code: Select all

Zend/Db.php
Zend/Controller/Front.php
and then the classes:

Code: Select all

class Zend_View {}
class Zend_View_Helper {}
But when I look at some of the example applications (including the one in the zend download) you see the files:

Code: Select all

/application/controllers/IndexController.php
/application/models/Album.php
and class:

Code: Select all

class Album extends Zend_Db_Table { }
But shouldn't that be class:

Code: Select all

class Application_Models_Album  extends Zend_Db_Table { }

Why no more mapping of class names to directories?

And on a related note. If I have three directory models/, controllers/ and views/ and for each "entity" I have classes in each of those, how should I call the files and the classes?
because to me

Code: Select all

models/Album.php
with class Album {}
controllers/Album.php
with class Album.php
and
views/Album.php
with yet another class Album {}
seems a bit confusing.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm completely unfamiliar with the ZF, but I think I can answer your question regarding the class naming. Controller, View and Model all typically have their own locations. It would be less organised to put them all into a lib or classes directory where additional classes are supposed to go. Within those directories you would name you classes according the pear naming conventions, so:

/application/models/Forum/Post.php

Code: Select all

class Forum_Post {}
/application/models/Forum/Thread.php

Code: Select all

class Forum_Thread {}
Like I say, I'm not familiar with the ZF though.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think it all depends on where you define "base" to be. You can either define a single base for everything and have longer class names, or move the base dir down in places. I think you should be able to tell a framework which one you want to do.
(#10850)
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

But the problem with naming them like your example chris, is that you end up with classes having the same name:
/application/models/Forum/Post.php

Code: Select all

class Forum_Post {}
/application/controllers/Forum/Post.php

Code: Select all

class Forum_Post {}
/application/views/Forum/Post.php

Code: Select all

class Forum_Post {}
Not so sure about the working of __autoload() and if that would create problems, but it certainly is not a clear naming to me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

This seems more like a Theory level question, especially considering the general discussion mantra of "no programming questions." ;)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

matthijs wrote:But the problem with naming them like your example chris, is that you end up with classes having the same name:
/application/models/Forum/Post.php

Code: Select all

class Forum_Post {}
/application/controllers/Forum/Post.php

Code: Select all

class Forum_Post {}
/application/views/Forum/Post.php

Code: Select all

class Forum_Post {}
Not so sure about the working of __autoload() and if that would create problems, but it certainly is not a clear naming to me.
I'm not sure about other frameworks, but at least in the Zend Framework, I never run into this particular problem because controllers are named like this:

/application/controllers/ForumPostController.php

Code: Select all

class ForumPostController extends Zend_Controller_Action {}
If you needed for your controllers to reside in multiple directories, you'd simply tell your front controller about all of them, and it would look in those directories.

and your views are named like this:

/application/views/forum/post.phtml

Code: Select all

/* template code would go in here... no class definition*/
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

@ninja, Yes, I saw that in a few of the examples. But that still leaves the model classes. And besides, were are the View classes (php I mean). I understand that the templates themselves can have an extention like phtml. But besides those there are the view logic classes, in the directory /views/.

@feyd: if this is supposed to go in theory & design, that's ok. When posting I felt it was a too simple question to post it there ..
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

matthijs wrote:And besides, were are the View classes (php I mean). I understand that the templates themselves can have an extention like phtml. But besides those there are the view logic classes, in the directory /views/.
Are you referring to helpers?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You don't often have a class for each view. Usually your view will just be a template with some PHP (or other language) in it. You would usually have *a* View class which helps carry model data from the controller to the template, then as ~Ninja points out you'll have view helpers to make common template snippets easier to include. A seperate class for each view is most likely not needed. That's not to say you won't need some way to create "viewable" component outside of your standard view.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Don't forget that ZF (at least when I last used it, close to a year ago admittedly..) requires you to define the location of your controllers before you can load any.

This is to minimise the chance of including the incorrect file - I can only assume - and the class(es) are infact 'included' explicitly, circumnavigating the autoloader completely, thus the need for the class naming convention mapping to the file structure is obsolete.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

Jenk wrote:This is to minimise the chance of including the incorrect file - I can only assume - and the class(es) are infact 'included' explicitly, circumnavigating the autoloader completely, thus the need for the class naming convention mapping to the file structure is obsolete.
You'd still have the chance of a nameclash though, since you can't redefine a class in PHP, and it doesn't have namespaces.

I wonder why the specification doesn't mention anything about filename case. Maybe all Zend developers are on windows machines?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

It does (or did) have a name mutator along the lines of:

Code: Select all

private function _controllerName ($name) {
    return ucfirst(strtolower($name)) . 'Controller';
}
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

@ninja: yes, kind of.
d11wtq wrote:You don't often have a class for each view. Usually your view will just be a template with some PHP (or other language) in it. You would usually have
*a* View class which helps carry model data from the controller to the template, then as ~Ninja points out you'll have view helpers to make common template snippets easier to include. A seperate class for each view is most likely not needed. That's not to say you won't need some way to create "viewable" component outside of your standard view.
Ok, then maybe my approach should be changed. I had started following the general setup on mvc found on phppatterns. Although with a few modifications, as kyberfabrikken pointed out, in that example the controller chooses the model for the view. In my setup, I let the view choose the model.

So back to naming, as the zend standards are not entirely clear on this, maybe I could choose a scheme:

Code: Select all

// controller class in /app/controllers/
class Users {}  
// model class in /app/models/
class UsersModel {}
// view class in /app/views/ (if needed)
class UsersView {}
I guess my issue is that I follow this approach with classes for controller, model and view, while Zend doesn't have that.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Zend adopted the PEAR Coding Standard for the library. However you need to distinguish between the library and the application. Controllers and Models carry most of an applications code and are exempt from following the PCS. This is for good reasons much of the time (nobody wants to really play with Default_Models_User when a AppUser would do as well with less typing).

Also, the framework has no specific autoloader for Models - there are only conventions in place for naming Controller classes. View templates have their own convention - /views/scripts/{controllerName}/{actionName}.phtml which is defined in the View->Controller integration layer introduced late in 1.0.0RC1 (read up on the ViewRenderer action helper). Controller naming conventions are simple AdminController, UserController - or if you use additional modules - Admin_UserController, Admin_SettingsController.

View helpers follow the convention of either using the default namespace (Zend_View_Helper_) or you can define a class prefix when adding custom helpers. The custom helpers are not required to follow a filesystem bound convention since they are not autoloaded - like Models it just removes the complexity of adding yet another class filesystem hierarchy.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Thanks for the explanation Maugrim.

Maybe I should clarify that I'm not specifically using the zend framework in this case but I just want to follow the same standards, in case I will use (parts of) the framework at some point. If not for compatibility then at least for consistency.

As I started adding more classes to a project I discovered that not having a good naming standard will get the code library very messy :)
Post Reply