Developer Forums | About Us | Site Map


Useful Lists

Web Host
site hosted by netplex

Online Manuals

Getting started with objects with PHP V5
By Matt Zandstra - 2005-08-30 Page:  1 2 3 4 5 6

A first class

You can create a class with the class keyword. At its simplest, a class consists of the keyword class, a name, and a code block:

class Dictionary {


The class name can contain any combination of letters and numbers, as well as the underscore character, but cannot begin with a number.

The Dictionary class in the previous example is perfectly legal, even if it is of limited use. So how do you use this class to create some objects?

$obj1 = new Dictionary();
$obj2 = new Dictionary();
$obj3 = new Dictionary();

In form at least, instantiating an object is similar to calling a function. As with a function call, you must supply parentheses. Like functions, some classes require that you pass them arguments. You must also use the new keyword. This tells the PHP engine that you wish to instantiate a new object. The returned object can then be stored in a variable for later use.


Within the body of a class, you can declare special variables called properties. In PHP V4, properties had to be declared with the keyword var. This is still legal syntax, but mainly for the sake of backward compatibility. In PHP V5, properties should be declared public, private, or protected. You can read about these qualifiers in Keywords: Can we have a little privacy in here? But for now, declare all properties public in the examples. Listing 1 shows a class that declares two properties.

Listing 1. A class that declares two properties

class Dictionary {
    public $translations = array();
    public $type ="En";

As you can see, you can declare a property and assign its value at the same time. You can get a quick peek at the state of an object with the print_r() function. Listing 2 shows that a Dictionary object now has more to it.

Listing 2. A look at the Dictionary object

$en = new Dictionary();
print_r( $en );  

If we run this script, we'll see output of:

Dictionary Object
    [translations] => Array

    [type] => En

You can access public object properties using the object operator '->'. So $en->type means the $type property of the Dictionary object referenced by $en. If you can access a property, it means that you can set and get its value. The code in Listing 3 creates two instances of the Dictionary class -- in other words, it instantiates two Dictionary objects. It changes the $type property of one object and adds translations to both:

Listing 3. Creating two instances of the Dictionary class

$en = new Dictionary();
$en->translations['TREE'] = "tree";

$fr = new Dictionary();
$fr->type = "Fr";
$fr->translations['TREE'] = "arbre";

foreach ( array( $en, $fr ) as $dict ) {
    print "type: {$dict->type} ";
    print "TREE: {$dict->translations['TREE']}n";

The script outputs the following:

type: En TREE: tree
type: Fr TREE: arbre

So the Dictionary class is now a little more useful. Individual objects can store distinct sets of keys and values, as well as a flag that tells a client more about the kind of Dictionary this is.

Even though the Dictionary class is currently little more than a wrapper around an associative array, there is some clue to the power of objects here. At this stage, we could represent our sample data pretty well, as shown in Listing 4.

Listing 4. Sample data

$en = array(
    'translations'=>array( 'TREE' => 'tree' ),

$fr = array(
    'translations'=>array( 'TREE' => 'arbre' ),

Although this data structure fulfills the same purpose as the Dictionary class, it provides no guarantee of the structure. If you are passed a Dictionary object, you know it is designed to have a $translations property. Given an associative array, you have no such guarantee. This fact makes a query like $fr['translations']['TREE']; somewhat hit and miss, unless the code making the query is sure of the provenance of the array. This is a key point about objects: The type of an object is a guarantee of its characteristics.

Although there are benefits to storing data with objects, you are missing an entire dimension. Objects can be things, but crucially they can also do things.

View Getting started with objects with PHP V5 Discussion

Page:  1 2 3 4 5 6 Next Page: Methods

First published by IBM developerWorks

Copyright 2004-2017 All rights reserved.
Article copyright and all rights retained by the author.