Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Using PHP Objects to Access Your Database Tables (Part 1)

By Tony Marston
2005-04-05


An introduction to OO functionality within PHP

Here is a brief overview of the Object Oriented functionality that is available within PHP 4. It is not intended to cover all the possibilities, just the essentials to get you going.

Creating a Class with Properties and Methods
A class can be created using code similar to the following:

class Foo

{
var $foo;
var $bar;

function setBar ($bar)
{
$this->bar = $bar;
}
function getBar ()
{
return $this->bar;
}
}
This class has the following characteristics:

The name of the class is 'Foo'.
It contains the variables (properties) '$foo' and '$bar'.
It contains functions (methods) called 'setBar' and 'getBar'.
Function 'setBar' is used to insert data into the object variable '$bar'.
Function 'getBar' is used to retrieve data from the object variable '$bar'.
In theory you are supposed to have a 'set' method and a 'get' method for each variable within the class, one to put data in and the other to get data out. These are commonly referred to as 'setters' and 'getters'.

Note here that the syntax $this->bar is used to reference an object variable. These variables can be referenced by any function within the class. The syntax $bar identifies a variable whose scope is limited to the current function only.

The 'constructor' method
A class can contain a special method known as a 'constructor' which is automatically processed whenever an instance of that class is created. In PHP 4 this is a method which has the same name as the class. This can be used to set initial data for the object, as shown in the following example:

class Foo

{
var $foo;
var $bar;

function Foo ()
{
$this->foo = 'initial value for $foo';
$this->bar = 'initial value for $bar';
}
}
In PHP 5 you can use the standard name __construct() as the constructor.

Extending a Class
It is possible to create a new class which 'extends' an existing class. By this I mean that you can inherit all the properties and methods of the existing class, and either provide alternative code for existing methods or add completely new methods. An example of how to do this is shown below:

require_once 'foo.class.inc';

class Bar extends Foo
{
var $tom;
var $dick;
var $harry;

function setTom ($tom)
{
$this->tom = $tom;
}
function getTom ()
{
return $this->tom;
}
}
Note that you have to include the definition of the parent class before you can extend it.

Class Bar is now an extension of class Foo. It has the following characteristics:

It has all the properties and methods of class Foo plus some properties and methods of its own.
If class Bar contained a method with the same name as a method within class Foo then the Bar method would replace the Foo method.
If class Bar does not have a constructor of its own then the constructor in class Foo will be used instead.

Creating an Object
Now that we have created a class how do we use it? The first step is to create an instance of the class known as an object. Note that you must include the definition of the class before you can create an object from that class, as shown below:

include 'foo.class.inc';

$object = new Foo;
Here the object is called '$object', but I could have used any name. Note that it is possible to create more than one object from the same class:

include 'foo.class.inc';

$tom = new Foo;
$dick = new Foo;
$harry = new Foo;
Accessing an Object's Properties and Methods
In order to perform a method within an object you need to specify both the object name and the function name as in:

$result = $tom->setFoo('value');
It is also possible to access an object's properties directly without going through a method, as in:

$var = $tom->Foo;

$tom->Foo = $var;
Although this approach is perfectly valid I should point out that if at some time in the future you decide that it is necessary to do some extra processing on the data before it is moved in or out of your object then you will have to modify all those places where the data is referenced. On the other hand if you force all object properties to be accessed though a get or set method then you will only have to change the contents of that method just the once.

You may have noticed that when you are outside of an object and you want to access the object's properties or methods you must specify the object's identifier as in $tom-> or $dick-> or $harry->, but when you are inside an object you can use the magic word $this-> as the object identifier.

Objects and Sessions
You may or may not be aware that you can maintain data between the execution of one script and another by using PHP's session capability. It is also possible to save an object's properties in this session data so that it can be reinstated by the next script within the same session. You can save an object's properties by using the serialize() command as follows:

include 'foo.class.inc';

$dbobject = new Foo;
...
...
$_SESSION['dbobject'] = serialize($dbobject);
In a subsequent script you can reinstate the object to exactly the same condition by using the unserialize() command like this:

include 'foo.class.inc';

if (isset($_SESSION['dbobject'])) {
$dbobject = unserialize($_SESSION['dbobject']);
} else {
$dbobject = new Foo;
} // if


Tutorial Pages:
» Intended Audience
» Prerequisites
» An introduction to OO functionality within PHP
» My 'database_table' class
» Using this Class
» Standard functions
» Summary


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1