How to organize PHP code?

This is a discussion on How to organize PHP code? within the PHP Language forums, part of the PHP Programming Forums category; I've been learning about PHP for a couple of weeks. With includes, PHP scripts, and HTML I can see ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-30-2003
Bruce W...1
 
Posts: n/a
Default How to organize PHP code?

I've been learning about PHP for a couple of weeks. With includes, PHP
scripts, and HTML I can see where a large and complex website could
easily turn in to a big hairy mess with files all over the place.

Are there any adopted standards, recognized recommendations, or best
practices on how all the code should be organized? I haven't found any
websites that discuss this.

Can anyone point me to information on this? If not then what do you do,
i.e. if you are organized?

Thanks for your help.
Reply With Quote
  #2 (permalink)  
Old 09-30-2003
AndyJ
 
Posts: n/a
Default Re: How to organize PHP code?

I have not found one definative way, but read equally good things
about minimal (code-seperate) and maximal (code-inline) styles.

Personally for small pieces I put them inline.
For repeating pieces I create functions and store in seperate file
using include() when neccessary, grouping like functions together.

Thats pretty much how I organise my projects. Inline is useful for
display stuff and calculations should be done in functions IMHO.

Hope that helps,

Andu Turner.


On Tue, 30 Sep 2003 17:32:40 -0400, "Bruce W...1"
<bruce@noDirectEmail.com> wrote:

>I've been learning about PHP for a couple of weeks. With includes, PHP
>scripts, and HTML I can see where a large and complex website could
>easily turn in to a big hairy mess with files all over the place.
>
>Are there any adopted standards, recognized recommendations, or best
>practices on how all the code should be organized? I haven't found any
>websites that discuss this.
>
>Can anyone point me to information on this? If not then what do you do,
>i.e. if you are organized?
>
>Thanks for your help.


Reply With Quote
  #3 (permalink)  
Old 10-01-2003
Pieter Linde
 
Posts: n/a
Default Re: How to organize PHP code?


You can use templates, have a look at this site.
http://smarty.php.net/

On Tue, 30 Sep 2003 17:32:40 -0400, Bruce W...1 wrote:

> I've been learning about PHP for a couple of weeks. With includes, PHP
> scripts, and HTML I can see where a large and complex website could
> easily turn in to a big hairy mess with files all over the place.
>
> Are there any adopted standards, recognized recommendations, or best
> practices on how all the code should be organized? I haven't found any
> websites that discuss this.
>
> Can anyone point me to information on this? If not then what do you do,
> i.e. if you are organized?
>
> Thanks for your help.


Reply With Quote
  #4 (permalink)  
Old 10-01-2003
Randell D.
 
Posts: n/a
Default Re: How to organize PHP code?


"Bruce W...1" <bruce@noDirectEmail.com> wrote in message
news:3F79F678.D77F4ABC@noDirectEmail.com...
> I've been learning about PHP for a couple of weeks. With includes, PHP
> scripts, and HTML I can see where a large and complex website could
> easily turn in to a big hairy mess with files all over the place.
>
> Are there any adopted standards, recognized recommendations, or best
> practices on how all the code should be organized? I haven't found any
> websites that discuss this.
>
> Can anyone point me to information on this? If not then what do you do,
> i.e. if you are organized?
>
> Thanks for your help.


I read an old C++ book years ago and since the programming is structured
similar, I've carried some of its suggestions with me... One being any
functions I write are rarely ever larger than a screen shot/display (ie no
need to scrool up/down to view a single function). Functions help you
locate bugs and, while I am not 100% certain of this, I do believe that
functions make better memory usage. This is because once a function
returns/completes, its allocated memory is made free again. The only
difference to this rule is if your function has been passed data, processed
and returned in near completion.

Secondly, I'm working on a project at the moment and other than inline print
statements (for example, to print values in to form fields that were
restored from my database) I have all my functions in a seperate file that
gets included before HTML manages to kicks in. This is in part because of
headers which much be sent before any html is sent - thus if you want to use
cookies, delivered by PHP, if you try to set a cookie after a piece of html
code you'll run in to errors.

Thus, for ever html file that I have, I also have a seperate php file which
gets included...

Oh! and lastly - if its your first time programming... make great usage of
remark statements in your code so when you reinspect it in weeks or months
time, or if someone else is working on your code, that it gives you/them an
easier life having to see what its supposed to be doing.

Hope that helps...


Reply With Quote
  #5 (permalink)  
Old 10-01-2003
rush
 
Posts: n/a
Default Re: How to organize PHP code?

"Bruce W...1" <bruce@noDirectEmail.com> wrote in message
news:3F79F678.D77F4ABC@noDirectEmail.com...
> I've been learning about PHP for a couple of weeks. With includes, PHP
> scripts, and HTML I can see where a large and complex website could
> easily turn in to a big hairy mess with files all over the place.


Maybe you could use some template system to keep your html an php separate,
it can help a lot to clean the code. TemplateTamer wiki has a number of
examples that wil get you started, and you can also look at other template
engines. (try searching in google for php template.

rush
--
http://www.templatetamer.com/



Reply With Quote
  #6 (permalink)  
Old 10-01-2003
Justin Koivisto
 
Posts: n/a
Default Re: How to organize PHP code?

Bruce W...1 wrote:
> I've been learning about PHP for a couple of weeks. With includes, PHP
> scripts, and HTML I can see where a large and complex website could
> easily turn in to a big hairy mess with files all over the place.


been there, done that.

> Are there any adopted standards, recognized recommendations, or best
> practices on how all the code should be organized? I haven't found any
> websites that discuss this.


Nor have I.

> Can anyone point me to information on this? If not then what do you do,
> i.e. if you are organized?


Over the years I have developed a library of functions and classes I've
used in project before. In that last year, I started to create myself a
CMS system to organize all this stuff and make me more efficient at
putting things together.

The way I organize things now is as follows:

config.ini.php: This file holds all the settings that I will be using
throughout the project. (Database connection details, image sizes, email
headers, etc.)

template.php: Parses config.ini.php and creates a configuration array,
initializes the database connection through db abstraction (usually
Metabase), and sets up an object of a class that holds the database
connection as well as some other helpful functions (like a formatHTML
function that pulls from the database, searches for HTML/non-HTML and
combines them for display). This file also parses the URI and/or
QueryString to find where in the database to look for the information.
Depending on what that URI/QueryString needs, other files may be
included. output buffering is used to store all the page's output into a
collection of variables.

include/*: This directory holds a collection of files that will be
included in the project somewhere. For instance, in my CMS, you will
find files like: normal.inc.php, news.inc.php, staff.inc.php, etc. - one
for each type of page supported by the CMS. There are also files like
update.normal.inc.php, etc. - update functions for each of the page
types. You would also find files like form.date.start.php - a file that
looks for a certain date variable, parses it, and creates 3 select menus
(year, month, day).

_site_components/*: In this directory I keep things like the db
abstraction package files, my security system used for the CMS, a folder
for images used on the site, a folder for each css definitions, js
scripts, flash movies, etc.

design.php: This file is included at the end of the template.php file.
This holds the HTML design of the project. Generated displays are then
echo'd where they are needed.

This approach keeps my HTML and PHP separate. Additionally, it keeps
closely-related control procedures and functions separate from others.
By doing this, I don't have all the code included into all requests,
only the parts that are needed. (Actually, my main class isn't separated
like that, but I've been thinking of doing so.)

Just my $.02

--
Justin Koivisto - spam@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Reply With Quote
  #7 (permalink)  
Old 10-02-2003
William L. Berggren
 
Posts: n/a
Default Re: How to organize PHP code?

"Bruce W...1" <bruce@noDirectEmail.com> wrote in message news:<3F79F678.D77F4ABC@noDirectEmail.com>...
> I've been learning about PHP for a couple of weeks. With includes, PHP
> scripts, and HTML I can see where a large and complex website could
> easily turn in to a big hairy mess with files all over the place.
>
> Are there any adopted standards, recognized recommendations, or best
> practices on how all the code should be organized? I haven't found any
> websites that discuss this.
>
> Can anyone point me to information on this? If not then what do you do,
> i.e. if you are organized?
>
> Thanks for your help.


I would suggest looking a pear php coding standards. Basically use 4
spaces to indent code within brackets. Yes, things can get messy.
But, for me each page only requires 3 files. A main, template and a
table function. The template is reused for all pages.
Reply With Quote
  #8 (permalink)  
Old 10-02-2003
Michael Willcocks
 
Posts: n/a
Default Re: How to organize PHP code?

I have only caught the last few lines of this thread (sorry if this has been
spoken about)

Is there software out there to format your code. as in add tabs where it
needs tabs, delete trailing spaces etc?

I use editplus and it does a very simple manual code format..


tia

"William L. Berggren" <bill.berggren@padobe.com> wrote in message
news:290c5e5f.0310011816.38155bb7@posting.google.c om...
> "Bruce W...1" <bruce@noDirectEmail.com> wrote in message

news:<3F79F678.D77F4ABC@noDirectEmail.com>...
> > I've been learning about PHP for a couple of weeks. With includes, PHP
> > scripts, and HTML I can see where a large and complex website could
> > easily turn in to a big hairy mess with files all over the place.
> >
> > Are there any adopted standards, recognized recommendations, or best
> > practices on how all the code should be organized? I haven't found any
> > websites that discuss this.
> >
> > Can anyone point me to information on this? If not then what do you do,
> > i.e. if you are organized?
> >
> > Thanks for your help.

>
> I would suggest looking a pear php coding standards. Basically use 4
> spaces to indent code within brackets. Yes, things can get messy.
> But, for me each page only requires 3 files. A main, template and a
> table function. The template is reused for all pages.



Reply With Quote
  #9 (permalink)  
Old 10-03-2003
Bruce W...1
 
Posts: n/a
Default Re: How to organize PHP code?

"Randell D." wrote:
>
> I read an old C++ book years ago and since the programming is structured
> similar, I've carried some of its suggestions with me... One being any
> functions I write are rarely ever larger than a screen shot/display (ie no
> need to scrool up/down to view a single function). Functions help you
> locate bugs and, while I am not 100% certain of this, I do believe that
> functions make better memory usage. This is because once a function
> returns/completes, its allocated memory is made free again. The only
> difference to this rule is if your function has been passed data, processed
> and returned in near completion.
>
> Secondly, I'm working on a project at the moment and other than inline print
> statements (for example, to print values in to form fields that were
> restored from my database) I have all my functions in a seperate file that
> gets included before HTML manages to kicks in. This is in part because of
> headers which much be sent before any html is sent - thus if you want to use
> cookies, delivered by PHP, if you try to set a cookie after a piece of html
> code you'll run in to errors.
>
> Thus, for ever html file that I have, I also have a seperate php file which
> gets included...
>
> Oh! and lastly - if its your first time programming... make great usage of
> remark statements in your code so when you reinspect it in weeks or months
> time, or if someone else is working on your code, that it gives you/them an
> easier life having to see what its supposed to be doing.
>
> Hope that helps...

==============================================

That makes a lot of sense. I'm coming from experience with ASP.NET
where each page has one code-behind file. And common settings like a
database connection string are kept in a web.config file.

The web.config file would be equivalent to Justin's config.ini.php
further down in this thread.

This general paradigm is easily done with PHP, and for that I'm glad to
see.
Reply With Quote
  #10 (permalink)  
Old 10-03-2003
equus
 
Posts: n/a
Default Re: How to organize PHP code?

This structure works for me...

/webroot
/bin (all binary files, mostly images)
/cron (all php & shell cron scripts relating to the current site)
/dev (scripts and images under development - this is a mess)
/doc (documentation, mostly small text files explaining stuff. also
contains change log. i go here when i forget what i last did)
/etc (configuration, i.e. database un&pw, etc - restricted perms)
/export (database dumps, usually with a cron)
/home (this is where most of the website lives)
/inc (included files)
/func (included functions)
/class (included objects)
index.php

So in the webroot there is just one file, index.php. All others are
categorized. Of course subdirectories may be required. For instance, I
usually chuck icon images into an /icons folder under /bin. It works for
me :)

It's funny that everyone I taught PHP to also use this structure or a
similar variation. I have seen some using a /scratch folder for pages that
won't live on the site for long, maybe an info gathering form for
instance. There's a robots.txt that prevents the pages from being indexed.

../Albe

On Tue, 30 Sep 2003 17:32:40 -0400, Bruce W...1 wrote:

> I've been learning about PHP for a couple of weeks. With includes, PHP
> scripts, and HTML I can see where a large and complex website could
> easily turn in to a big hairy mess with files all over the place.
>
> Are there any adopted standards, recognized recommendations, or best
> practices on how all the code should be organized? I haven't found any
> websites that discuss this.
>
> Can anyone point me to information on this? If not then what do you do,
> i.e. if you are organized?
>
> Thanks for your help.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:13 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0