Wednesday, August 12, 2009

SETTING UP PHP5

All recent major Linux distributions (SUSE, Fedora, Mandriva, and Debian among them) come with support for PHP 5. If your distribution doesn’t support version 5, the easiest solution is to locate and install an updated Red Hat Package Manager (RPM). Otherwise, you will need to download the PHP source code and configure and install PHP your- self. (If you want to install PHP as a static module, you will also have to down- load the source code for Apache.) Instructions for compiling PHP are readily available at http://php.net, but taking this route requires familiarity with working from the command line in Linux.
PHP 5 also runs under Windows using Internet Information Server (IIS) or Apache Web Server. Although Windows does not come with built-in support for PHP, installing PHP is a relatively easy task. The Windows PHP installer will get you up and running in minutes, but it is not meant for a production server—it’s better to perform a manual installation. Comprehensive instruc- tions for doing this are provided at http://php.net/install, but here’s a brief overview of the process.

Download the Windows binary from the PHP website, and install it to a directory on your hard drive. If you are using IIS, find the web server config- uration window and map the .php file extension to the location of the php program.
For Apache Web Server 2, you will need to make the following changes to the httpd.conf file:

LoadModule php5_module "c:/php-5.1/php5apache2.dll"

If you are running version 1.3 of Apache, use the php5apache.dll file.
You will also have to add an application type to your configuration file. The example below will process files with the extensions .php or .inc as PHP files.

AddType application/x-httpd-php .php .inc

Comprehensive instructions for installing and configuring Apache under Windows can be found at http://httpd.apache.org/docs/2.0/platform/ windows.html, but, again, the process is fairly straightforward.
The code contained in this book should run equally well regardless of which combination of operating system and web server you choose.

php.ini Settings

The php.ini file controls configuration settings for PHP and is typically found in the c:\windows directory on Windows systems and in the /etc directory on Linux systems. When installing PHP 5 it is best to use the example php.ini file with the default settings. This section deals with changes that affect OOP. (For an overview of all the changes, see http://php.net/install.)
There is only one new configuration setting that relates directly to changes made to the object model in PHP 5. Specifically, showing the default setting, this is:

zend.ze1_compatibility_mode = Off

If you change this setting to On, objects will be copied by value in the manner of PHP 4. (See the section “__clone” on page 116 for more details about how objects are copied.) This option is provided in order to facilitate migration from PHP 4 to PHP 5. It should be used for this purpose only, as it is unlikely to be supported in any upcoming versions of PHP.
Another setting that has some bearing on changes made to the object model in PHP 5 is

allow_call_time_pass_reference = Off

This setting controls whether a warning is issued when a variable is passed by reference when making a function call. With this setting off, calling a function in the following way will issue a warning and will not pass
$some_variable by reference:

some_function(&$some_variable);

The recommended way of passing a variable by reference is by declaring the parameter as a reference in the function definition, like so:

function some_function ( &$some_variable ) {
...
}

If you do this, then there is no need to use an ampersand when passing a variable to some_function. (If you are upgrading PHP 4 code that passes objects at call time by reference, you can remove ampersands entirely. You will recall that in PHP 5 objects are automatically passed by reference, so there is no need for an ampersand at call time or in the function definition.) It is a good idea to upgrade your code to pass by reference in the recommended manner because call-time pass by reference is unlikely to be supported in future versions of PHP.

E_STRICT

A new error level, E_STRICT, has been introduced and is especially useful in the context of OOP. If you set error reporting to this value, a warning will be issued when deprecated functions or coding styles are used. Error level E_ALL does not encompass E_STRICT, so include this error level explicitly in the php.ini file in the following way:

error_reporting = E_ALL|E_STRICT

To see how this setting can be useful, suppose, in the style of PHP 4, that you do the following:

$obj1 =& new Person();

With error reporting set to E_STRICT and display_errors set to on, you’ll receive the message:

Strict Standards: Assigning the return value of new by reference is deprecated...

Other actions also raise a warning when error reporting is set to E_STRICT:

Use of is_a instead of instanceof.
Invoking a non-static function statically (this error is soon to be E_FATAL).
However, calling a static method against an instance variable does not raise a warning.
Use of var instead of public, private, or protected (prior to version 5.1.3).
Changing the number of parameters or the type hint when overriding a method in a derived class.

Making sure that your code follows strict standards can help ensure that it is forward compatible especially with respect to calling dynamic methods statically.

Don’t Escape Twice

There’s a final setting that has some bearing on OOP.
It’s worthwhile noting that the default setting for magic quotes is

magic_quotes_gpc = Off

As you have seen, methods such as the prepare method of the PDO class automatically escape database queries. So, if magic quotes are turned on, you can easily corrupt data by escaping it twice. Use care if you change this setting.