Americas

  • United States

Even more Zen of PHP

Opinion
Mar 24, 20034 mins
Enterprise ApplicationsPHP

* Gearhead columnist Mark Gibbs brings you PHP's object model

Are you sitting comfortably? Then we’ll begin . . . . Actually we’ll begin with an error: Last week editing gremlins obfuscated a line in the section where we showed PHP code that demonstrated variables changing type with wild abandon. We won’t print the full text but simply note that in PHP “single line comments can be prefixed with either “#” or “//” and multiline comments can be framed with “/* … */”.

OK, so last week we promised you PHP’s object model. We should note that an object is a load of code and variables that are wrapped up such that all you can usually access are the object’s public functions. In the object-oriented programming world these functions are called methods, and whatever variables are public are referred to as properties.

In PHP, things are more liberal because there is no mechanism to enforce the privacy of the methods and properties of an object. This is good because there isn’t the associated computational overhead required to protect the internals of an object, but it also is bad because poor programming could violate and corrupt the object model.

In PHP (and in many other languages) you create objects from classes. A class is a template from which a specific object – an instantiation – is created. You can create multiple objects from the same class, so if we had a class called “horse_class” with properties of “name” and “color,” we could create multiple objects to represent specific horses. Here’s our class declared in PHP:

class horse_class

{

var $name=””;

var $color=””;

function getDetails()

{

print “The horse’s name is $this->name and it is $this->color”;

}

}

This class provides variables to hold data and a single method that outputs those details on request. The special variable $this stands for the object that $this is within, letting us access the properties and methods local to the object. Note that we’re outputting HTML, which can be as complex as you please. OK, let’s create some objects:

for ( $count=1; $count

{

$horse[] = new horse_class();

}

Here we’ve used a loop that indexes from 1 to 10 and creates an array of 10 horse_class objects (PHP automatically creates the array index so it is unnecessary to write “$horse[$count]”).

Another cool feature of PHP arrays is that you can assign your own index value. By default, array indexes start at 0, but you can assign any value as the index for each element. Thus, you can have an index that starts from 1 or any other number, or you can use string values. This last feature is very useful, as you can create “associative” arrays where the index strings act as keys. For example, let’s create a 2-D array element (effectively an array in an array):

$animal[90] = array (

name=>”Casey”,

species=>”dog”,

color=>”gold” );

If we were to access $animal[1][species] the value “dog” would be returned.

There’s a lot more to PHP arrays. You can get the size of an array, loop through either a regular or an associative array, add, insert and remove elements, concatenate and break up arrays, and sort the elements numerically or alphabetically. Let us now set the properties of the horse objects:

$horse[1]->name = "Dorothy";

$horse[1]->color = “chestnut”;

$horse[2]->name = “Winston”;

$horse[2]->color = “gray”;

To print the properties of the objects in the horse array we could use:

foreach ($horse as $temp)

{

if ( $temp->name != “” )

{

$temp->getDetails();

}

}

This code indexes through the elements of the array of objects called $horse, skipping all objects that don’t have their name property set and printing out the details of each that does using the object’s method. Again, there’s a lot more to PHP objects – check out the PHP manual’s Classes and Objects chapter.

We could fill many more columns with an exploration of PHP nuts and bolts, but there’s really no substitute for actually playing, er, working with code.

Next week: a great PHP development system. Details to gearhead@gibbs.com

mark_gibbs

Mark Gibbs is an author, journalist, and man of mystery. His writing for Network World is widely considered to be vastly underpaid. For more than 30 years, Gibbs has consulted, lectured, and authored numerous articles and books about networking, information technology, and the social and political issues surrounding them. His complete bio can be found at http://gibbs.com/mgbio

More from this author