[thelist] PHP Organization

Hans Zaunere zaunere at yahoo.com
Wed Jul 10 15:13:09 CDT 2002


--- Beau Hartshorne <beau at members.evolt.org> wrote:
> Hi,
>
> I'm trying to figure out a good way to organize all of my .php code.
> I'd also like to find a good way to write it so that it's easy to
> understand, and then modify later.

Use libraries, or "class" files in OOP parlance.

> Until now, I've kept most of my code in large files. It doesn't take
> long before this gets really messy, then later hard to read and
> modify.
> Part of this massive file would have some code that printed out a
> form,
> some code that checked the form's contents once it was submitted,

form.inc.php

> some code to insert this information in a database.

db.inc.php

> Instead, I'd like to keep smaller chunks of code seperated in
> different
> .php files, and use one "brain" .php file per major section (product
> catalogue, contact, etc) to figure out which of the smaller .php
> files to include.
> I think a good way to organize this would be to keep
> separate directories for each section, and keep all of the .php files
> that are unique to that section in their directory.

No, don't segregate on "section", as what defines a "section" will
change, especially between projects.  Divy your code up based on the
functionality it provides.

> The "brain" .php
> file would be the index.php file inside whatever directory. So, the
> directory structure would look something like this:
>
> /index.php
> /contact/index.php
> /contact/print_contact-form.php
> /contact/check_contact-form.php
> /contact/update_contact-database.php
> /products/index.php
> /products/show_catalogue.php
> /products/show_item.php
>
> etc...

Ick.  make something like /usr/local/apache/phplib (outside of your web
tree) and add it to your include_path (or use absolute paths).

Then, any script that deals with contact, place
require_once('contact.inc.php'); at the top of the script.
contact.inc.php then in-turn requires db.inc.php, form.inc.php,
somethingnew.inc.php, etc - whatever it needs.  The same goes for
products.inc.php, etc.

> There will probably be a few things that the entire app would need
> access to. They could be kept in a /global directory.

Make /usr/local/apache/phplib/common.inc.php

>
> Another advantage is that you could code links like this:
> <http://www.foo.com/bar/> instead of: <http://www.foo.com/bar.php>.

Keep your functionality and presentation seperate.  There are other,
much better, ways to use extensionless URLs.

Hans Z.
New York PHP
http://nyphp.org


__________________________________________________
Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free
http://sbc.yahoo.com



More information about the thelist mailing list