PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 5 ■ OBJECT TOOLS

Neither solution is ideal. By specifying paths in this much detail, you freeze the library file in place.
In using an absolute path, you tie the library to a particular file system. Whenever you install the
project on a new server, all require statements will need changing to account for a new file path.
By using a relative path, you fix the relationship between the script’s working directory and the
library. This can make libraries hard to relocate on the filesystem without editing require() statements
and impractical to share among projects without making copies. In either case, you lose the package
idea in all the additional directories. Is it the business package, or is it the projectlib/business package?
In order to make included libraries work well in your code, you need to decouple the invoking code
from the library so that


business/User.php


can be referenced from anywhere on a system. You can do this by putting the package in one of the
directories to which the include_path directive refers. include_path is usually set in PHP’s central
configuration file, php.ini. It defines a list of directories separated by colons on Unix-like systems and
semicolons on Windows systems.


include_path = ".:/usr/local/lib/php-libraries"


If you’re using Apache you can also set include_path in the server application’s configuration file
(usually called httpd.conf) or a per-directory Apache configuration file (usually called .htaccess) with
this syntax:


php_value include_path value .:/usr/local/lib/php-libraries


■Note .htaccess files are particularly useful in web space provided by some hosting companies, which provide


very limited access to the server environment.


When you use a filesystem function such as fopen() or require() with a nonabsolute path that does
not exist relative to the current working directory, the directories in the include path are searched
automatically, beginning with the first in the list (in the case of fopen() you must include a flag in its
argument list to enable this feature). When the target file is encountered, the search ends, and the file
function completes its task.
So by placing a package directory in an include directory, you need only refer to packages and files
in your require() statements.
You may need to add a directory to the include_path so that you can maintain your own library
directory. To do this, you can, of course, edit the php.ini file (remember that, for the PHP server module,
you will need to restart your server for the changes to take effect).
If you do not have the privileges necessary to work with the php.ini file, you can set the include path
from within your scripts using the set_include_path() function. set_include_path() accepts an include
path (as it would appear in php.ini) and changes the include_path setting for the current process only.
The php.ini file probably already defines a useful value for include_path, so rather than overwrite it, you
can access it using the get_include_path() function and append your own directory. Here’s how you can
add a directory to the current include path:


set_include_path( get_include_path().":/home/john/phplib/");


If you are working on a Windows platform, you should use semicolons rather than colons to
separate each directory path.

Free download pdf