How do you organize your PHP scripts and files?

I do literally exactly the same as Dachande663

Keep it very simple

Wow, great thread; I’m right in the middle of a massive script reorganization project right now. Unfortunately, I scarcely understand anything people are suggesting on this thread. Guess I need another two years of PHP experience. :slight_smile:

But I basically organize my scripts according to function and location - i.e., do they affect something in the head section, the top of the page, the body, the “central body” where I display articles and images, or whatever?

I’m trying to develop a system that allows several websites to share a central repository of PHP scripts. Every website uses the same footer, which is all mapped out on a single file, footer.php. But websites with unique features obviously require unique scripts.

But I’m trying to standardize things as much as possible. For example, each website will have a Topics section, with the main Topics page therefore linked to the same file. However, articles will be stored in separate database tables for each website. Therefore, I substitute a variable for the table name in my query, then use a simple PHP switch to input the correct table name…


switch($Site)
 case 'MySite1':
 $Table = 'MySite1';
 break;
 case 'MySite2':
 $Table = 'MySite2';
 break;
 default:
 break;
}

Do you have to understand OOP to get any use out of a program like CodeIgniter or Symfony?

I’ll never understand why you bloat a simple conditional that could be done in a single line to an 11 line switch… anyway… no. These are all object oriented frameworks.

I follow the ZF file organization, which is very clear to me, even for my own framework.

Agreed. I follow the ZF naming, coding, and testing conventions. It keeps my projects clean and uniform even if I’m not using the ZF. :slight_smile:

I generally work to the MVC principal but not actually using the framwork - i say that because i split everything up so i have all templates in one file, classes in another and modules in another and my index.php file normally ends up being quite short. Its easy to get to the desired files then.

I read through it
http://framework.zend.com/manual/en/coding-standard.html

I’m not strict with it, but I’ve picked up some habits I learned there. At least it helped me better understand the importance of consistency in my code and I have my own conventions now. I recommend anyone else to have a look.

I have a pretty simple folder structure for my projects

/class <– classes
/doc <– documentation
/etc <– config files
/func <– stand-alone functions
/inc <– other include files (usually just a file that had definitions for HEADER and FOOTER constants, and a few other things that I don’t think belong in my config files. Sometimes other stuff ends up here too)
/www <– web root
/www/images <– duh
/www/cssjs <– css and js files, and for some reason I always stick random things that don’t belong in here

I use my own framework and other frameworks such as zend

/server/ <—root directory for security reasons all 3rd party code is installed/runs only here, everythign is run under reduced privelege user:group with apparmor thrown around!

/server/tmp/ <–temp cache used by all applications, uses ramdisk for extra speed!

/server/var/ <–root directory for my and 3rd party frameworks
/server/var/zend_1.0.1/
/server/var/myframework_0.9.0/

/server/servers/ <–mysql, php, lighttpd, nginx and so on is manually compiled with required packages only

/server/apps/ <–root directory for web applications

/server/apps/www.example.com/ <–root directory for web application project

/server/apps/www.example.com/httpdocs/ <–http root folder for the application

/server/apps/www.example.com/bin/ <–scripts such as cron, sockets servers and so on

/server/apps/www.example.com/tmp/ <–application only cache

/server/apps/www.example.com/modules/ <–application modules (controllers)

/server/apps/www.example.com/classes/ <–application classes (models)

/server/apps/www.example.com/var/ <–various items for the application such as error pages, master templates and so on

However the project I’m working on organizes them.

I’m pretty flexible.

I have studied how many foss projects implement folder structure.

Drupal has one of the cleanest, most orderly Ive come across.

Definitely worth a look imo.

I notice that lots of folks use the one-file-per-class idea, which is great, but the file names are often “className.class.php”…and they all reside underneath a single “classes/” directory. The same goes for files like “inc/fileName.inc.php”.

Why do you bother with these redundant file names if all the files are stored under appropriately named directories? Just curious…

The following directory structure works pretty well for me:


project
    * app
          o controllers
                + NewsController.php
                + …
          o etc
                + schemas
                + config.ini
          o jobs
          o models
                + NewsModel.php
          o plugins
          o templates
                + news
                      # add.html
                      # edit.html
                      # list.html
                + …
          o application.php
    * libs
          o Smarty
          o Zend
          o …
    * public_html
          o css
          o images
          o js
          o .htaccess
          o bootstrap.php
    * tests
          o NewsTest.php
    * tmp
          o cache
          o templates_c

You don’t have to do anything , but to know what do UML means …

http://www.google.com/search?q=uml

Wow. I’m surprised by how many directories people have in their directory structures.

I haven’t got into the whole MVC thing (I don’t quite get it or the advantages of it - seems just like the ‘in’ thing at the moment). I could be persuaded though, but it seems awfully bloated to me.

I use:


/project
/project/lib
/project/lib/classes
/project/httpdocs
/project/httpdocs/images
/project/httpdocs/includes
/project/httpdocs/scripts
/project/httpdocs/styles

(substitute httpdocs for public_html, wwwroot or whatever the server I’m using requires - obviously this is the directory the domain ‘points’ to)

In my lib folder I have an include file, generally inc.projectname.php, which then includes all of the other functions / general classes / config file / constants definitions / initialisation scripts (which go in lib) and project specific classes (which go in lib/classes). The httpdocs folder is pretty self explanatory. I sometimes separate my content images from my design / layout images as well with a content/ folder inside the images folder. includes/ has all the inc.header.php, inc.footer.php, inc.nav.php etc - that are all just include()'d by the scripts themselves. Scripts has any javascript files i might use, styles any css files (usually use a main one for layout, then a separate file for each separate part of an application, like a calendar or something).

I guess one thing about it is that all the classes are loaded on every page - but I don’t see this as a great bottleneck. Surely the limiting factor is the downloading of the final result by the client, rather than how much php is executed? I just love the simplicity of being able to include a single file at the top of every httpdocs page (require_once(‘…/lib/inc.projectname.php’):wink: - I could try autoloading though I guess?

I find it interesting so many PHP developer have jumped into the MVC model, it’s good to see.

A genral problem, thanks for replies, they helped me.

I’m with you. When you want to use a php file you include it in the calling page, so all php files fall into the category “includes”.

It’s like all things in object oriented programming. You’re trading complexity for low coupling and high reusability.

It all depends on the size of your project. I only use PHP 5 OOP if it is a big application and want encapsulation, because unnecessary OOP slows down your app. Keep it simple and develop your own style. Why be a follower, when you can be an innovator. Think about what you are trying to accomplish and do what YOU think works best. Function files or libraries work just as good as class files or libraries organization. The key is to save time by reusing code as much as possible.

Most of my projects are pretty lightweight so I keep it very simple.
function files and application files occasionally I will call a function from the function file I wrote for a different application file and usually there is a function file that contains all of the sites connection and authentication functions that I call from nearly all the application scripts.
If job is more complex than that I go with Zend, but I am considering CI, Cake, and Sym.