473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.
Jul 17 '05 #1
10 11041
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"
<br***@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.


Jul 17 '05 #2

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.


Jul 17 '05 #3

"Bruce W...1" <br***@noDirectEmail.com> wrote in message
news:3F***************@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...
Jul 17 '05 #4
"Bruce W...1" <br***@noDirectEmail.com> wrote in message
news:3F***************@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/

Jul 17 '05 #5
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 - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #6
"Bruce W...1" <br***@noDirectEmail.com> wrote in message news:<3F***************@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.
Jul 17 '05 #7
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" <bi***********@padobe.com> wrote in message
news:29**************************@posting.google.c om...
"Bruce W...1" <br***@noDirectEmail.com> wrote in message

news:<3F***************@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.

Jul 17 '05 #8
"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.
Jul 17 '05 #9
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.


Jul 17 '05 #10
"Michael Willcocks" <mi*****@metrogroup.com.au> wrote in message news:<3f***********************@lon-reader.news.telstra.net>...
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

I use Kate text editor. It highlights php. It is a KDE linux
program.
It is hard to explain but Linux Format magazine rates
it a high 9 for text editors. It also has text-to-speech
capabilities, spell check and some other weird stuff. But the best
part is you can have 10 files
open at once and cut and paste between them.
Jul 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: Greger | last post by:
Dear all, what do you people use for organising code for optimal compile time? I have an app , gui + core + file ops, which is growing and compile time is too long, need a better way of organising...
1
by: Rudderius | last post by:
Hey, I'm working on a winform app in VS 2005. I'm wondering if someone knows some best practices on how to organize the toolstrips. I have a menuToolStrip and some other toolstrips. As in many...
4
by: Daniel N | last post by:
I am new to .net and want to organize my code better. I am writing in vb.net and the code for my main form is nearing 50-60 pages and would like to create another file like a class, module or code...
2
by: key9 | last post by:
Hi all look at the organize tree main.c ------ #include lib_adapter.c main() { foo();
6
by: PythonNewbie | last post by:
Hello, I am trying to organize code that a student had written. Currently it is one really long file of different classes which makes it very tough to follow things. I would prefer to have...
1
by: Thomas Wittek | last post by:
Hi! Is there any possibility/tool to automatically organize the imports at the beginning of a module? I don't mean automatic imports like autoimp does as I like seeing where my...
3
by: mpjones2 | last post by:
I'm trying to organize some code I've written and was wondering if anyone can provide any suggestions on an equivalent to #Region that can be used within a function to collapse sections of code...
11
by: Daniel Norden | last post by:
Hello. Can you give me some feedback on my code layout? I have everything organized in an object, "NRL", that's used as a namespace, and there are other subobjects/modules below that, for...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.