. 39
( 132 .)


You can add to and delete from this array.
Chapter 7: Writing Organized and Readable Code 207

We™ve said it before, and we™ll say it again: Use phpinfo() to get informa-
tion about variables in your environment or your configuration.

In PHP a global variable is not automatically available within a function. If you
want to use a global within a function you must indicate within the function that
the variable you are accessing is a global.
Here is an example of using a global within a function:

function add_numbers($val_2)
global $number;
echo $number + $val_2;

$number = 10;

This code prints 15. Here $number is a global because it is assigned outside of a
function. Using the keyword global tells PHP that you want to fetch the specified
number from the $GLOBALS array. The preceding code can also be written like this:

function add_numbers($val_2)
echo $GLOBALS[“number”] + $val_2;;
$number = 10;

In the applications in this book we use the technique shown in the first example
because it seems a little cleaner, and because directly manipulating the $GLOBALS
array is not really encouraged. It™s nice to see where your variable is coming from
at the top of the function.
Within your functions, you might want to make variables available as globals.
That way they are available in the body of your script and in other functions. You
can create a global variable the same way you access a previously defined one, with
the global keyword. Here™s a quick example:

function assign_to_global($val_1, $val_2)
global $sum;
208 Part II: Working with PHP

$sum = $val_1 + $val_2;

echo $sum;

This script prints 11. For something a bit more complicated, we borrow the fol-
lowing function from the applications section of the book:

function set_result_variables ($result)
if (!$result) { return; }
$row = mysql_fetch_array($result,MYSQL_ASSOC);
while (list($key,$value) = each($row))
global $$key;
$$key = $value;

This function expects a result identifier gathered by mysql_query() in an ear-
lier function. Assume that the query run prior to this function call returns a single
row. That row is then assigned to an associative array named $row. Then each col-
umn taken from the query (which is now the key in the associative array) and its
value are available as a global. This availability can be useful if the values retrieved
from the query are needed in many other functions. However, beware of having
columns with the same names as PHP variables ” particularly global variables. You
should try to not let that happen, lest conflicts occur.
Global variables are used sparingly within functions throughout the applications
in this book. This is because it is easier to keep track of your variables if you are
passing them through arguments and retrieving them through return values. If you
start using globals extensively you might find that your variables are returning
unexpected values in different places ” and finding the functions that are causing
the error can be a major pain.
Here™s another reason to avoid globals when possible: You will be using the
same variable names over and over and over again. We don™t know how many
times in these applications the variable names $query, $result, $row, or $i are
used, but trust us when we say that they are used frequently. All kinds of hassle are
introduced if you have to keep track of each time you use a variable name.
At times you have little choice but to use global variables, but before you do,
make sure that you can™t accomplish what you™re trying to do using variables of
local scope.
Chapter 7: Writing Organized and Readable Code 209

Object-Oriented Programming
A few years back there was a large move toward object-oriented programming.
Some people thought that the procedural approach ” that is, coding strictly with
functions ” just wasn™t enough. Therefore, the folks working on languages like C++
and Java popularized an approach that enables a developer to think about code in
a different way.
The idea behind object-oriented programming is to think of portions of your
application as objects. What is an object? Well, it™s an amorphous thing, a kind of
black box ” a superstructure of variables and functions. But if you are new to the
concept, this description may not be so clear.
To make things clearer conceptually, we provide a few examples. In our exam-
ples, our objects are things that can be displayed as part of a Web page. For each of
the different kinds of elements of a page, you might want to know different things.
If you™re displaying an image, for example, you might want to know how wide it is.
For an HTML table, you might want to know if it has a color. By treating the ele-
ments as objects, you can effectively ask them how wide they are or what color
they are without having to dig inside them to find out yourself. You can also tell
the elements to do things, like draw themselves on the page.
Now all you need is the correct nomenclature. Descriptions of the object (such as
width and color) are called properties, and descriptions of the actions an object can
take (such as draw) are known as methods. Some methods are a bit of both, and you
can think of them as descriptions of the questions you can ask the object ” a
width() method might tell you how wide the object is and whether it has one
property named width or a dozen different properties that it has to add together
first. And as it happens, in the actual code of a class, you use the word function
just as you would in a regular user-defined function, so you might hear both terms
used interchangeably. Here™s an example of what a PHP class looks like:

class TextBox
var $_text;

function TextBox($text)
$this->_text = $text;
function style()
return “font-family:™Helvetica Neue™,Helvetica,sans-serif;”;
function text()
210 Part II: Working with PHP

return <<<EOT
<font style=”{$this->style()}”>{$this->_text}</font>

Before you get to using objects, however, we want to explain a couple of the
advantages of this object-oriented approach. Suppose some programmer has cre-
ated an object and tells you about its methods and properties. You don™t really need
to know how any of it works; you just need to know that it does. You can make use
of the methods and properties of the object in your scripts with little effort.
Of course, the same could be said of a well-designed procedural approach. A
well-documented collection of functions can work equally well. Time was when
objects in PHP weren™t much more than collections of function libraries and arrays
that used little arrows instead of square brackets. That has changed dramatically
with PHP 5, and the language is now much closer to other object-oriented languages
like Java. However you look at it, you should be able to write good procedural code
before you move on to objects.
By using objects, not only can you make use of methods and properties in the
heart of your scripts, but also you can extend the functionality of a class in a num-
ber of different ways, with one of the most basic being the use of a concept called
inheritance. Going back to the previous example, for example, an image and a table
both might have a height and a width. We can create a class called a Rectangle
that knows about heights and widths and then have our Image class and Table
class inherit Rectangle, automatically being able to make use of all its properties
and methods. (In object terminology, we call Rectangle the parent class, and
Image and Table are both children.)
There™s a lot more to object-oriented programming (or OOP for short ” not to be
confused with OOPS, which is the kind of programming we™re trying to avoid) than
just inheritance, though. We can™t do full justice to the topic here, but we™ll try to
cover some of the basics.

Classes, Continued
In object-oriented programming, you work with classes and objects. A class is a
definition describing properties and methods, while an object is a variable that has
those properties and can use those methods. You could think of the blueprints for
the chair you™re sitting in now as a kind of Chair class. That would make your
actual chair an object, or instance, of the Chair class. If your chair came from a
factory, there might be thousands of Chair instances out there in the world, but
still only one Chair class.
You can use a few different kinds of classes. Let™s start out our small example
with one of the most minimal kinds of classes, an interface.
Chapter 7: Writing Organized and Readable Code 211


interface Color
public function color();
interface Drawable
public function draw($return=false);
interface Rectangle
public function height();
public function width();

Not much code there. But then that™s the point ” an interface isn™t about how
you will do something, so much as what you will be able to do. An interface is like
a promise: Any class that builds on the Color interface, for example, swears that it
will have a method named color with no arguments that you will be able to call.
In this case, we™re not promising anything about what that method will do (or what
it will return to you, for that matter). But if you™re dealing with an object that has
Color in its background, you know that you™ll be able to call $object->color()
and get some kind of response.
So what does that get you besides a new buzzword? Well, without claiming to
describe all of the benefits, here are a couple of basic ones.
In PHP, normal classes are inherited, while interfaces are implemented, like so:

class AlertBox extends TextBox implements Color, Rectangle

Here, extends means that the AlertBox class is inheriting the properties and
methods of the TextBox class. You can inherit only from a single class in PHP, but
you can implement as many interfaces as you like. The benefit comes from the fact
that you can tell if a particular object implements the interface in which you™re
interested, using the instanceof operator. Say that you™ve got a section of code
that doesn™t care about anything but rectangles. Circles, triangles, lines of text ” let
somebody else deal with those; we just want rectangles. You can make that work
like this:

if ($object instanceof Rectangle)
$width = $object->width();
$height = $object->height();
212 Part II: Working with PHP

To write the same code without the concept of an interface, you™d have to do
something like this:

$methods = get_class_methods($object);
if (in_array(˜width™, $methods) and in_array(˜height™, $methods))
$width = $object->width();
$height = $object->height();

Even then, you don™t know if that object™s width() method is going to require
an argument that you can™t supply. Plus, clearly, this is going to be a lot slower to
run. The Rectangle interface tells you very simply that this is a proper Rectangle
that knows how to respond to a decent width() call, regardless of what other kinds
of foolishness it might get up to somewhere else.
Another benefit of using interfaces can come when you have multiple people
working on different parts of the same project. By laying out a set of interfaces as
the first step, each person can write code that will call on the other classes being
built by other people, whether or not those other classes have actually been written
yet, and without worrying about how they™re going to work when they are written.
We can take that approach one step further, to specific properties and methods,
by moving on to inheritance and abstract classes.

An abstract class is sort of a blueprint™s blueprint. It looks just like a regular class,
because that™s what it is, except for one difference: You can™t make an object out of it.
To add another $5 word to our pile, we can say that an abstract class cannot be
instantiated ” meaning that you can™t create an instance of one, at least not directly.
Instead, abstract classes are there solely to be inherited. One way you can use
them is as a place to put utility code ” code that all of your classes will use, but that
doesn™t really have a purpose outside of the specific context of one of those classes.
Maybe your abstract class has a method that does some complex mathematical cal-
culations or one that picks one string at random from an array of strings.
PHP considers a class to be abstract if you explicitly declare the class that way ”
abstract class MyClass { ... } ” or if one of the class™s methods is declared to
be abstract ” abstract function some_function();. Another, indirect way of
making a class abstract is by declaring that it implements an interface without sup-
plying one or more of the methods that the interface defines. The effect is to shift
the burden of supplying those required methods onto the class™s children.
Here™s another step in our example that shows what we mean: an abstract class
that implements one of the interfaces we declared previously ” except not really:


. 39
( 132 .)