View Full Version : PHP Code Organization

08-25-2011, 10:45 PM
Hey everyone,

How do you organize your code? I code in objected oriented PHP and I often find my self shuffling around my files during development because it gets pretty messy(I don't use a framework btw).

I tend to create classes for my display files(functions that output HTML) and put them in one folder, the program logic (database connections, if statements) in another folder, and finally the interface(I/O, what did the user click on a certain page) in a final folder.

Does anyone do something similar? Am I organizing code properly? And what do you guys recommend for a more structured/clean development organization-wise?

I'm curious to hear your thoughts.


08-25-2011, 11:47 PM
I think it depends on how you and your team if you have a team like to work.

There are many purists out there who swear by MVC design. There are others, like myself, who have all the php in one folder, images in another, css in another, and then js in another folder.

I think the best answer to that is how you personally like your structure. So long as you know what folders have what, the rest usually works itself out.

08-26-2011, 05:18 PM
I don't do either of these. I used to do N-Tier style setup which is similar with interface (actual front end), business (rules invoked by front end control), and data access (invoked by business). Since 5.3 is now the new standard by Zend, this has shifted from an n-tier model to a namespace based model still built with the n-tier in mind. Since these are namespaced and the namespace can be chained and handled by __autoload in PHP 5.3+, I can now arrange my directories as I see fit (when invoking a using: use \path\from\root\namespaces\MyClass AS MyClass, it will automatically load the definition for MyClass).

08-26-2011, 06:30 PM
I do something even screwier but just as effective.

I run everything through one index.php file which handles session checking, logins etc. Any custom functions are stored in utils.inc.php and any html is stored in <pagename>.inc.htm and the same goes for html components which may need multiple loops from php - such as a <select> menu. It's broken into seperate files one for the main <select> and then another for the rows.. which PHP reads into a string, puts its values into and then loops again.. once I have all the items that string is then merged into the file for the main <select> container (which is then put into the main html and printed).

The only other thing I keep in seperate files is modules which are basically plugins for my program. Those live in the modules folder. Oh and the mysql driver.. which can detect which machine its on my its host name return the correct settings for that SQL server and then connect to it.

Thats it.. simplez!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum