Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
Thread: PHP Code Organization
08-25-2011, 09:45 PM #1
- Join Date
- Sep 2010
- The Twilight Zone
- Thanked 2 Times in 2 Posts
PHP Code Organization
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, 10:47 PM #2
- Join Date
- Mar 2010
- Thanked 6 Times in 6 Posts
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, 04:18 PM #3
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,660 Times in 2,629 Posts
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, 05:30 PM #4
- Join Date
- Feb 2011
- Your Monitor
- Thanked 526 Times in 513 Posts
- Blog Entries
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!
See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/
Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!