Page 1 of 2

Site Layout \ foundation : how do you do yours?

Posted: Sun Nov 05, 2006 10:02 pm
by nameless1
ok so here is the question how does everyone manage their sites in terms of layout and directories and constants \ how much oop other people use and so on.

Also one of the biggest most important aspects of creating a foundation for my sites I think is a simple one how do you access the directories.


I dont like the idea of C:\blah\blah in windows and /blah/blah in unix so i wrote a function to convert the output of __FILE__ to unix style directory

I am aware of DIRECTORY_SEPARATOR constant but I chose not to do that approach due to the fact directory things will be stored in other places and i would prefer it to be consistent. dirname combined with the converted file is how i have managed finding out directories to help set constants.

But how many constants are most people using for directories alone

I find i am using

CHARLES_DIR <-- name of my library i am making (location of it)
ROOT_DIR <-- root
TEMPLATE_DIR
CONFIG_DIR
CLASS_DIR
AVATAR_DIR
IMAGE_DIR

PHP is '.php';

Also i am using Constants for heaps of <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> even the session stuff just because USER_LOGGED is easier and clearer

require_once ROOT_DIR . 'test.php' . PHP;

I have also beeing using the following dir structure
(charles dir is my framework)
(program entry points go in /libs/ everything else gets grouped into a directory if there is more then one file

/libs/charles/
/libs/charles/db/
/libs/charles/template/
/libs/charles.php

/templates/selected/
/config/
/images/avatars/
/

most of the work i have done lately has been on my framework so i will skip over that part but most of my code has

Charles_Template <-- my name prefix
Charles_Extended_Template <-- i am extending all my classes within reason (as in most of the time 90%) just because i can store the configs and data source in an extended version of the class.
Charles_Template_Exception <--exception

because my extended class has an ugly name i am placing it insided a registry and i access the template
through public static template that looks something like below

$t = Charles::Template();
$t->assign('name', 'value');
$t->display('file.tpl');

so how is everyone else managing their site foundation \ base

oh i have also been preferring to write my own library\framework just due to the fact it is lighter as i only add what i need in many cases its simpler and more abstract when i write it myself. I am also doing it mostly for the educational point of view. But do you reject frameworks just because you dont like the idea of relying on other peoples code?? Its for this reason I will refuse to use frameworks that are made by anyone but me unless its a joint collaboration i am a part of

oh and i also use autoloader for everything but the framework im doing but i use spl_autoload to set it in my class

Posted: Sun Nov 05, 2006 10:12 pm
by feyd
Constants: few and far between.

My general directory structure, although slightly different right now, can be found here: viewtopic.php?p=252208#252208

Some further reading on my views of formatting, code style and other such personal preferences can be read here: viewtopic.php?t=46500&highlight=code+styl%2A

And there's no need to continue swearing in your posts.

Posted: Sun Nov 05, 2006 10:18 pm
by nameless1
the swear filter on this site is a little to strict and you took it out of context because of the bleeper thing.

Posted: Sun Nov 05, 2006 10:20 pm
by feyd
Context has no baring on whether it's welcome here or not. :?

Posted: Sun Nov 05, 2006 11:03 pm
by alex.barylski
Slightly off topic...but...feyd I didn't see any swear words? :?

Whatever, doesn't matter...your smurfing...even for me the "village hellraiser" is getting annoying...when you cuss to much you loose credibility...sound like a gangsta' which I assure you none of us are ;)

Anyways...

My designs...

- Extremely strict coding conventions, but they serve a purpose...
- Single application entry point (index.php)
- phpDocumentor comments
- Charts, diagrams, use cases, etc
- Security policies (SQL injection, XSS attacks, session fixation prevention and so on)
- Optimization rules
- A directory structure which is in constant flux and varies from project to project

Most of the above have been documented in my own coding standards document...I'm pretty anal about everything so it's getting to be quite verbose...I have found the less questions I have to ask myself during development the fast I actually program or write code, etc...letting me spend more time designing.

I automate as much as possible by using convention. Using a front controller which loads appropriate command handlers, which execute events or load pages, etc...

Same goes for SQL...my table fields share 1:1 naming relationship with FORM fields when possible...modularization and seperation are keys to my development.

HTML from PHP...SQL from Language files, etc...

Yes I use OOP extensively, but only when it makes sense...when what I'm dealing with is actually an object...or when I feel the need for a namespace calling functions statically...otherwise I still use procedural code when I can...slightly less overhead.

TDD is about the only thing I haven't fully integrated with my development life cycle...but I'm working on it...

Cheers :)

Posted: Sun Nov 05, 2006 11:12 pm
by nameless1
About the swearing thing <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> you :) lol i could not resist.... but If you really have a problem with it take up the issue with the politeness police because you are being so ... I would type s.t.u.p.i.d here but it would probably come up in the filter.

But to clarify what I am interested in knowing for the next poster. I was more interested in managing directory structure and loading classes and how people keep track of what dirs are where. I did this via a lot of constants and extending the classes to keep the configuration seperate from the main site I am just wondering if anyone has any good ways I have not thought of.

Posted: Mon Nov 06, 2006 12:01 am
by Christopher
I use a Config object, but usually all I set there is the Base URL and Base Path to the site. Usually I set PHP's include path to have one or more library directories outside of the site dir that I get code from -- but try to keep it simple.

Posted: Mon Nov 06, 2006 8:55 am
by MrPotatoes
Hockey wrote:My designs...

- Extremely strict coding conventions, but they serve a purpose...
- Single application entry point (index.php)
- phpDocumentor comments
- Charts, diagrams, use cases, etc
- Security policies (SQL injection, XSS attacks, session fixation prevention and so on)
- Optimization rules
- A directory structure which is in constant flux and varies from project to project

Most of the above have been documented in my own coding standards document...I'm pretty anal about everything so it's getting to be quite verbose...I have found the less questions I have to ask myself during development the fast I actually program or write code, etc...letting me spend more time designing.

I automate as much as possible by using convention. Using a front controller which loads appropriate command handlers, which execute events or load pages, etc...

Same goes for SQL...my table fields share 1:1 naming relationship with FORM fields when possible...modularization and seperation are keys to my development
my naming conventions prolly aren't as strict as yours but they are more than most people can handle i've noticed. for one i use very long variable names with humping on all first letters. the more descriptive the better. sometimes i even do it for iterators.

this is for me down the line. if i see $ParentControllerRoot as a variable name i'll never get it confused with $RootController and so on. alot of people don't share my views but it's my code and my company. tough dookies right? right

hey hockey, can i see some of your DD's? mine are mostly on paper and if it's on the computer it's un orgnized. i was thinking of going from white board to paper then HTML with hyperlinks and audio. but i think that's alittle over the top if you ask me.

oh, also. one entry point, lots of config files and lots of goodies hidden everywhere. i also make everything drop dead simple to understand. nothing over my head or anyone's head. the more complicated it is the harder it is to fix, change, itnegrate or have someone else understand in order to get them to help. all those goodies

Posted: Mon Nov 06, 2006 10:01 am
by Jenk
I was going to reply.. but I won't bother now (with the exception of this line..)

Posted: Mon Nov 06, 2006 10:18 am
by Chris Corbyn
nameless1 wrote:About the swearing thing smurf you :) lol i could not resist.... but If you really have a problem with it take up the issue with the politeness police because you are being so ... I would type s.t.u.p.i.d here but it would probably come up in the filter.
Listen buddy, I'm not sure who you think you are but you've joined this board with a cocky attitude and disrespecting what the moderators are asking of you doesn't exactly score well on your second day here. The "s" word you used (no I don't mean "smurf") is a swear word regardless of what context you're using it in. We have younger generations frequent these boards, we're a popular forum and we like to maintain some sort of respectable tone on the board. So are you going to listen or are you just going to argue? Ok, I've said my bit.

Carry on.

Posted: Mon Nov 06, 2006 10:30 am
by snowrhythm
alright, i'm kind of a newbie to php as i've only been using it for a few months, and i'm not sure i understand the big deal about
directory structure. so what's the big deal? what are constants used for? and why would you write code to create dirs?

Posted: Mon Nov 06, 2006 10:54 am
by Chris Corbyn
snowrhythm wrote:alright, i'm kind of a newbie to php as i've only been using it for a few months, and i'm not sure i understand the big deal about
directory structure. so what's the big deal? what are constants used for? and why would you write code to create dirs?
Basically, if you're building an application it may start out as a 3 page, easy to code and easy to maintain set of scripts.

Then your client asks for a couple more pages to be added, a new authentication system and some bells and whistles. Fine. Then a few more pages... and more... before you know it you're sinking in a jungle of hacked code with no real structure.

Directory structure is just a part of keeping things organised. Basically, don't be afraid to use lots of files to break your application down into smaller, more manageable components. In each file you'll typically find a class or a function named with respect to the name of the file itself.

So a typical layout may have directories for "classes", "libraries", "templates" etc. It just makes it easier to find where you'd have put something in 3 years time when you need to do some tweaking. Beyond that you may even have sub-directories for various modules (see PEAR naming conventions). It also makes it easier to load in components at runtime just by using a set of rules based upon your chosen structure. This becomes more apprent once you delve into the realms of OOP and make use of pattern like the Autoloader or Service Locator.

Constants are just a way of creating globally accessible fixed values which you cannot modify. The absolute path to your application (for includes) shouldn't change during the running of the application so this may be defined as a constant. Many people avoid them now since they are always cluttering up the global namespace and there are other ways of storing configuration data such as singleton or static class.

~nameless1:

Why worry about the directory separator? You've totally lost me there. PHP is cross platform and works with "/" regardless. Also, why do you have a constant for "PHP" ? I'd probably avoid using a constant name like "PHP" anyway since you never know when Zend may decide to use it themselves given its name.

I pretty much follow PEAR naming standards for the layout of my libs and classes (thanks ~arborint for the regular nudge on that one) and I use a tree based structure for paths within my application which link directly to records in a DB for each page; which in turn link to a set of permissions for each page; which in turn link to a RBAC system for the users.

Posted: Mon Nov 06, 2006 12:21 pm
by MrPotatoes
ok. some people use libraries and classes as a different folder? why? i've never done this so i don't know. what is different between a library and a class? i basically have them set up the same.

explain :)

Posted: Mon Nov 06, 2006 12:58 pm
by snowrhythm
As far as i can tell from what i've read, a library is a set of files (or it could be just one file i guess, but that might be considered bad structuring) where you store your functions or classes in order to call them later and keep them organized. So you can have a library for procedural programming or OOP. A class is used with object oriented programming (OOP), and the class holds the functions (but in a class context you call them 'methods', probably to help prevent confusion) and member variables, that is, variables that can be called from within any method. Classes are only used with OOP.

Posted: Mon Nov 06, 2006 1:08 pm
by Chris Corbyn
MrPotatoes wrote:ok. some people use libraries and classes as a different folder? why? i've never done this so i don't know. what is different between a library and a class? i basically have them set up the same.

explain :)
Library: A collection of components and interfaces providing extended functionality to the developer.

Class: A component itself acting as a container for properties and methods and an interface for an object.

You're right, there's not really a difference. It's just often that libs need to hold their own directory structure since the developer may have built them to work based upon that structure. We've also used schmes like "classes/3rd-party/<.....>". It's mostly just so that __autoload() can read from a nice cleanly organised directory.