| home / programming / phpanth2 / 1 | [previous] [next] |
|
|
Using the class externally is now even easier:
// Instantiate the page class
$webPage = new Page('As Easy as it Gets', date('Y'),
'Easy Systems Inc.');
// Add something to the body of the page
$webPage->addContent(
"<p align=\"center\">It's so easy to use!</p>\n");
// Display the page
echo $webPage->get();
?>
Essentially, the page is now built using only three lines of code; I can also reuse this class to generate other pages. Represented as a UML diagram, the Page class is shown in Figure 2.1.
The member variables appear in the middle area, while methods appear in the bottom box. Also, the plus and minus signs are there to indicate to other developers which elements of the class are public (+) and which are private (-). Unlike languages such as Java, PHP does not enforce privacy on objects;[3] in the examples above, we could have accessed the $page member variable directly in our main script. Because we want the object to handle its own data without outside interference, we indicate in the UML diagram that only those members that have a + against them are available for public use. Those with a - are purely for internal use within the class.
That covers the basics of the class syntax in PHP, and should give you an idea of how classes compare with procedural code. With the syntax you’ve learnt, you should be able to write standalone classes containing the variables and functions you use frequently—a task that can really help tidy up your code and make it easier to maintain. This is a great start, but the real power of object oriented programming comes from using multiple objects and classes together. The rest of this chapter will look at some of the more advanced facets of the PHP object model, including references, inheritance, aggregation, and composition.
Developers who are familiar with compiled languages such as C++ or Java should note that references in PHP are not analogous to pointers in other languages.
A pointer contains an address in memory that points to a variable, and must be dereferenced in order to retrieve the variable’s contents.
In PHP, all variable names are linked with values in memory automatically. Using a reference allows us to link two variable names to the same value in memory, as if the variable names were the same. You can then substitute one for the other.
By default, when a variable is passed to anything else, PHP creates a copy of that variable. When I say “passed,” I mean any of the following:
Passing a variable to another variable:
<?php $color = 'blue'; $settings['color'] = $color; ?>
$settings['color'] now contains a copy of $color.
Passing a variable as an argument to a function:
<?php
function isPrimaryColor($color)
{
// $color is a copy
switch ($color) {
case 'red':
case 'blue':
case 'green':
return true;
break;
default:
return false;
}
}
$color = 'blue';
if (isPrimaryColor($color)) {
echo $color . ' is a primary color';
} else {
echo $color . ' is not a primary color';
}
?>
When $color is passed to the function isPrimaryColor, PHP works with a copy of the original $color variable inside the function.
The same applies when passing variables to class methods:
<?php
class ColorFilter {
var $color;
function ColorFilter($color)
{
// $color is a copy
$this->color = $color;
// $this->color is a copy of a copy
}
function isPrimaryColor()
{
switch ($this->color) {
case 'red':
case 'blue':
case 'green':
return true;
break;
default:
return false;
}
}
}
$color = 'blue';
$filter = new ColorFilter($color);
if ($filter->isPrimaryColor() ) {
echo ($color.' is a primary color');
} else {
echo ($color.' is not a primary color');
}
?>
The original $color outside the class is passed to ColorFilter’s constructor. The $color variable inside the constructor is a copy of the version that was passed to it. It’s then assigned to $this->color, which makes that version a copy of a copy.
All of these means of passing a variable create a copy of that variable’s value; this is called passing by value .
To pass using a reference, you need to use the reference operator & (ampersand). For example:
<?php
$color = 'blue';
$settings['color'] = &$color;
?>
$settings['color'] now contains a reference to the original $color variable.
Compare the following examples, the first using PHP’s default copying behavior:
<?php
$color = 'blue';
$settings['color'] = $color; // Makes a copy
$color = 'red'; // $color changes
echo $settings['color']; // Displays "blue"
?>
The second involves passing by reference :
<?php $color = 'blue'; $settings['color'] = &$color; // Makes a reference $color = 'red'; // $color changes echo $settings['color']; // Displays "red" ?>
Passing by reference allows us to keep the new variable “linked” to the original source variable. Changes to either the new variable or the old variable will be reflected in the value of both.
So far, so good. You’re probably wondering, “What’s the big deal here? What difference does it make whether PHP copies or makes a reference to a variable, as long as we get what we expected?” For variables passed around a procedural program, you hardly ever need to worry about references. However, when it comes to objects interacting with one another, if you don’t pass an object by reference, you may well get results you weren’t expecting.
| home / programming / phpanth2 / 1 | [previous] [next] |
Created: March 27, 2003
Revised: January 2, 2004
URL: http://webreference.com/programming/phpanth2