Page 1 of 2
Zend framework coding standards
Posted: Tue Jul 03, 2007 1:33 pm
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.
Posted: Tue Jul 03, 2007 1:54 pm
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
/application/models/Forum/Thread.php
Like I say, I'm not familiar with the ZF though.
Posted: Tue Jul 03, 2007 2:46 pm
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.
Posted: Tue Jul 03, 2007 2:59 pm
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
/application/controllers/Forum/Post.php
/application/views/Forum/Post.php
Not so sure about the working of __autoload() and if that would create problems, but it certainly is not a clear naming to me.
Posted: Tue Jul 03, 2007 3:06 pm
by feyd
This seems more like a Theory level question, especially considering the general discussion mantra of "no programming questions."

Posted: Tue Jul 03, 2007 3:11 pm
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
/application/controllers/Forum/Post.php
/application/views/Forum/Post.php
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*/
Posted: Tue Jul 03, 2007 3:17 pm
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 ..
Posted: Tue Jul 03, 2007 3:24 pm
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?
Posted: Tue Jul 03, 2007 3:38 pm
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.
Posted: Tue Jul 03, 2007 3:58 pm
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.
Posted: Tue Jul 03, 2007 4:59 pm
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?
Posted: Tue Jul 03, 2007 5:22 pm
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';
}
Posted: Tue Jul 03, 2007 11:32 pm
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.
Posted: Wed Jul 04, 2007 2:38 am
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.
Posted: Wed Jul 04, 2007 2:54 am
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
