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.
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.
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.
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.
[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
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):
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.