How do you organize your PHP scripts and files?

Actually i want my PHP codes to be professionally organize,
Yeah, i can understand OOP, but how do you organize them properly?

Let say i have a very very BIG project, now what do you do for your first step
to organize your php script? then the second step,the third step and so on.

I want to hear your experiences.

How do you also organize the files and folders?

Thanks in advance.

As much as I love computer technology, nothing beats good old fashioned pen and paper.Before I even touch a keyboard I put everything on paper.

Much like database method I organise things into segments / sections generally by definition of category, depending on the particular project and the clients needs.

I keep my things one class per file, names className.class.php, similar classes grouped in a subfolder (if there are more than one), all subfolders in a classes folder which I place in the include path.
If I have reoccuring code in files I put that in a common, loader-type file, which I then include everywhere. This sits in a folder of it’s own, again in the include path.
Everything related to configuring a script is confined to one array, say $params[‘this_or_that’], which sits in a config file of it’s own, which sits in the include path.

May be difficult to organize at first, but once you have everything figured out, like a blank project it’s a simple copy-paste job to start a new one, comment out, or delete what you don’t need from the current job in the loader file and start coding.

Take a look at how zend framework is organised. I’m using something very similar - one file per class. Classes are categorised by their types - modules, controllers, views etc. Every category has its own subfolder in a class directory. As sebulbus said, every configurable thing must be put into one config file. I’m using constants for such things.
Now about a script, try to read some articles about MVC pattern. In a few ford it’s a way of programming, when data, it’s presentation and logic are separated. Of course, don’t forget about OOP principles and always think about what can be reused in other projects, make use of inherintance.

Typically, I don’t put everything in one class. I organize it into smaller classes. for example:
class.MySQL.php
class.TextArea.php
class.DropDown.php
class.SMTP.php
class.Login.php
class.Etc.php

I store all of these in a folder like: lib/php/classes

Then, I use autoloading so that I only include the classes I’m using for that particular script. For example, I don’t need to include the MySQL class if I’m not going to make a MySQL query on this page.

Once autoloading is set up, all I need to do is:


$t = new TextArea($var1, $var2, $etc);

Obviously, not all classes will be simple enough to implement in one line. But hopefully you see my point.

Study the PHP classes really good before starting: http://php.net/class

Well, I now use MVC using the Zend Framework, so my organization pretty much follows the conventions of ZF now.

Before that, I’d organize all my scripts by what their purpose was:
/ - the root of the application (not necessarily the server’s web root)
/bin - all my data processing scripts
/img - images and PHP scripts that produced images would go here
/inc - include files; only config scripts would be in the root of this subdirectory
/inc/classes - my class files (one class per file)
/inc/func - function files, grouped by type (i.e. similar functions in one file)
/template - my template files

All the scripts that the user would ever see would be in the application’s root; while for particularly large projects this sometimes got a little out of hand, for most projects this organization worked very well for me.

you guys are really advanced.

thanks guys.

MVC. I also do not use abbreviations for directory titles, so I have things like “include” and “library.”

/engine/ stores my controller classes, the business logic of the particular app names the model while the theme is usually also the name of the view.

Do any of you guys ever try to reorganize existing projects that were started by someone else?

Get an MVC framework.
It’s genius…no, it’s beautifully genius. :smiley:
It does all the work for you.

CakePHP :

Creating applications this way will win you peace, honor, women, and money beyond even your wildest fantasies. Simple, isn’t it?

i do my OOP design as others have mentioned, as for my file structure, i do something like this:

root/
-backup/
–database/
–filestystem/
–mail/
-classes/
–controllers/
-templates/
–html/
–email/
–rss/
–(etc)
-www/
–images/
–styles/

all classes get their own file, and i create subfolders based on whether i need it or not for the current project, but this is just a very minimal base for my projects.

Using CakePHP here:


[i]/cake[/i] - CakePHP libraries
[i]/app/models[/i] - models, each model is in its own file
[i]/app/views[/i] - views, each controller has its own subdirectory
[i]/app/controllers[/i] - controllers, each controller is in its own file
[i]/app/vendors[/i] - other classes and functions, file per class/library
[i]/app/config[/i] - configuration:
  [i]/core.php[/i] - CakePHP and application-specific configuration
  [i]/database.php[/i] - database connection configuration

MVC is the best structured program coding for PHP Scripts.

Try www.codeigniter.com

That is the best user guide that cut off your learning curve.

Code Igniter is a very promising project. It doesn’t overdo like most other PHP Frameworks.

I like to have a folder structure that mirrors the class hierarchy, sort of like is suggested in the PEAR coding standards (but not quite exactly like that, since I kind of feel like it’s overkill for a non-PEAR project). Since I don’t usually have too many levels of inheritance, it works sort of like this (folders in square brackets, files otherwise):

[classes]
–[Model]
----ConcreteModel1.class.php
----ConcreteModel2.class.php
–[Controller]
----ConcreteController1.class.php
----ConcreteController2.class.php
–Model.class.php
–Controller.class.php

One thing, however, doesn’t seem to fit very well into this system: interfaces! Since a class can implement more than one interface, it’s not really possible to store classes in folders based on their interfaces alone. So where do the interface files go?

So far my answer has always been to put them in a separate folder independent of the [classes] folder. I.e.,

[classes]
–(see above for structure)
[interfaces]
–iModel.php
–iController.php

This seems kind of messy to me, but I haven’t had any better ideas yet. Any suggestions?

I usually do one class/interface/function per file using the name as the file plus “.php”. I don’t use “.class.php” or anything like that because if “class” suddenly becomes “interface” I don’t want to have to keep renaming my files (just being lazy I guess).

As far as directory structure goes:


root
  public
    stylesheets
    images
    javascripts
    [etc.]
  private
    app
      models
      views
      controllers
      helpers
    lib
      [Basic framework classes, abstract classes, interfaces, etc]
    db
      [SQL files, XML files, etc]
      SQLite
        [SQLite database (*.db) files]
    config
      [YAML, XML, INI, etc files - one per area/category (main.yml, db.yml, etc)]
    tests
    contrib
      [Libraries/code that I did not write]
  temp
     cache
     logs
     sessions

The directory structure is pretty much a modified version of the one RoR uses.

By comparison I keep mine pretty simple:
/
-func
-views
-templates
-thirdparty

and that’s pretty much it.

I like Code Igniter as well, and want to get into it, I haven’t learned it yet, but the tutorials are nice and easy to learn.

I’ve also heard good things about CakePHP so might check that out as well.

CI and Cake are trash compared to Symfony. :wink:

apps/
  [app name]/
    modules/
      [module name]/
          actions/
          config/
          lib/
          templates/
          validate/
batch/
cache/
config/
data/
  sql/
doc/
lib/
  model/
log/
plugins/
test/
  unit/
  functional/
web/
  css/
  images/
  js/
  uploads/