Approaches to Web Development for Bioinformatics

Previous  Contents  Next
References

Object Oriented Programming in PHP 4

Object oriented programming first appeared in PHP 4. The implementation was fairly minimal until PHP 5. Therefore, I will split the discussion on object oriented programming into two sections: one on PHP 4 and the other on PHP 5.

Object types (classes) can be defined with the class keyword:


PHP

<?php
/*
* An object encapsulating gene information
*/
class Gene_Info {
var $id; // The NCBI identifier field
var $symbol; // The gene symbol
var $name; // The name of the gene

function Gene_Info($myid, $mysymbol, $myname) {
$this->id = $myid;
$this->symbol = $mysymbol;
$this->name = $myname;
}

function get_id() {
return $this->id;
}

function get_symbol() {
return $this->symbol;
}

function get_name() {
return $this->name;
}
}

header('Content-Type: text/plain;charset=utf-8');

// Create a few Gene_Info objects and put them in an array
$hd = new Gene_Info(3064, "HD", "huntingtin (Huntington disease)");
$sry = new Gene_Info(6736, "SRY", "sex determining region Y");
$geneList = array($hd, $sry);

// Iterate over the Gene_Info objects and print them out
$count = count($geneList);
for ($i = 0; $i <$count; $i++) {
echo $geneList[$i]->get_id() . "\t" . $geneList[$i]->get_symbol() . "\t" .
$geneList[$i]->get_name() . "\n";
}

?>

This example demonstrates a very language features. The name of the class or object type, Gene_Info, follows the class keyword. Inheritance can be used with the extends keyword but I will not demonstrate that. The Gene_Info class declares three member variables id, symbol, and name. The constructor is a method (function) with the same name as the class. It is invoked when a new object instance is created with the new keyword. Code within the class definition can use the $this psuedo variable to access member variables (not the absence of the $ for member variables). The program output is


Pogram output

3064 HD huntingtin (Huntington disease)
6736 SRY sex determining region Y

Objects can be serialized with the predefined function serialize(). This is useful for storing them in a user session. They can be unserialized with the predefined unserialize() function.


Previous  Contents  Next
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies