|
|
|
|
| Tutorial |
Using Files to Group Functions
We’ve shown that it pays to use multiple files for source code, but we encourage you
to also use files for most other resources, such as configuration data; custom headers,
footers, or other templates; and anything else that can be extracted from your project
as a separate entity.
Using multiple files for a single project offers quite a few advantages:
n You get smaller source code files that are easier to maintain.
n You can create different revisions for each file instead of having to check in the
whole project for a small modification.
n You can detach resources from the project and reuse them in other projects.
n Different team members can work on the project simultaneously, without having
the trouble of merging when checking all files into the revision control system.
These issues apply to most resources that can be present in a project.
Files should be named according to their contents, optionally with a prefix if a
bunch of files belong to a larger group and should be placed into subdirectories from
the project root. For example, in a database abstraction layer with modules for accessing
different databases packed into single files, each should be prefixed with dba_
(where dba stands for database abstraction), so that you’d get dba_mysql, dba_odbc,
dba_oracle, and so on.
Make sure that you can vary subdirectories later on by using configurable module
directories in your includes. For example (note that dba in this example doesn’t refer
to the PHP dba_* functions):
<?
require(“config.php3”);
require(“$dba_root/dba.php3”);
require(“$socket_root/socket.php3”);
require(“$phpPolls_root/phpPollUI.php3”);
// [...]
?>
The variables $dba_root, $socket_root, and $phpPolls_root in this example should be
contained in a central configuration file with global options for the whole project.
This configuration file should only contain options that are needed by every source
file independently, and thus have to be made globally available. Such options might
include environmental options such as the site name, file system locations, and so on.
Stay on the (Generic) Path
When including the configuration file from a subdirectory, always use relative paths to ensure that your
project is mobile on your filesystem and on your customers’ systems as well—never rely on special conditions
of your developing environment being present in all deployment environments as well. Whatever
you can keep generic should be kept generic.
|
|
|
|
|
|
| Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist |
|