Approaches to Web Development for Bioinformatics

Previous  Contents  Next
References

Object Oriented Programming in PHP 5

In PHP 5 the object implementation was rewritten and new capabilities introduced. The final keyword was introduced to prevent defining classes using inheritance (extends keyword). This can be important from a security perspective and to give clarity when publishing classes that are not intended to be overridden. The __construct() method is the new convention for defining constructors.

Visibility of methods can be declared using the keywords public, protected, and private (similar meanings to Java). Using the minimum appropriate visibility is a useful tool in scaling complex projects and a good practice to use from API publishing and security perspectives. The keyword var in the example above is a PHP 4 convention and equivalent to public in PHP 5. Method declarations without a visibility modifier are treated as public. The following program demonstrates some of these concepts using a modified version of the PHP 4 class above.


PHP

<?php
/*
* An interface to be implemented by all classes having a name
* and symbol.
*/
interface InfoObject {

public function getSymbol();

// What is your name?
public function getName();

}

/*
* A PHP5 object encapsulating gene information
*/
class GeneInfo implements InfoObject {
private $id; // The NCBI identifier field
private $symbol; // The gene symbol
private $name; // The name of the gene

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

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

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

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

public function __toString() {
return "{$this->id}\t{$this->symbol}\t{$this->name}";
}
}

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

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

// Iterate over the GeneInfo objects and print them out
$count = count($geneList);
for ($i = 0; $i <$count; $i++) {
print $geneList[$i] . "\n";
}
?>

The program additionaly demonstrates the use of the interface keyword. An interface defines the signature (or interface) of class. The class then implements the interface. This gives calling code a confidence that the methods declared in the interface will be there without having to worry about the details of the implementation. However, their is arguably limited reason for interfaces in a scripting language like PHP.

The class also demonstrates the use of the __toString() function. This is a standard function that determines how an object of that class type will be stringified. It can be convenient for logging purposes to keep the calling code cleaner. The program output is the same as the PHP 4 version.

The static keyword was also introduced in PHP 5. It has a similar meaning to Java. The const keyword (for constant) was also introduced in PHP 5 to define symbolic names for values that do not change.

The abstract keyword was introduced in PHP 5 to allow for definition of class interfaces without allowing instantiated. Abstract classes must contain at least one method that is marked abstract. These abstract methods must be overridden in a derived class for it to be able to be instantiated.

An additional keyword introduced in PHP 5 is the final class modifier. Use final when it does not make sense to override a class. Here is a script that demonstrates some of these concepts.


PHP

<?php

/**
* A base class for biological symbols like DNA, RNA, amino acid
* alphabets.
*/
abstract class Symbol {

public abstract function getName();

public abstract function getAnnotation();

}

/**
* A class representing the nucleic acid base Adenine
*/
final class Adenine extends Symbol {
const NAME = 'a';
const ANNOTATION = 'Adenine';
private static $instance;

private function __construct() {
}

public function getName() {
return self::NAME;
}

public function getAnnotation() {
return self::ANNOTATION;
}

public static function getInstance() {
if (self::$instance == NULL) {
self::$instance = new Adenine();
}
return self::$instance;
}
}

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

// Get the one and only instance of Adenine
$a = Adenine::getInstance();
print $a->getName() . "\t" . $a->getAnnotation() . "\n";

?>

The abstract class Symbol defines two methods, getName() and getAnnotation(), which child classes must implement. The child class Adenine extends Symbol and declares itself as final to indicate that it should not be extended itself.

The class Adenine declares two constants using the const keyword. These constants are accessed from inside the class with the self keyword to indicate the class (as opposed to this instance) itself and the :: operator to indicate a static context. To prevent calling code from instantiating the Adenine class the constructor is declared as private. To access the one and only (singleton) instance of the class calling code uses the static method getInstance(). Static fields and methods are associated with the class definition rather than instance objects. The output of the script is


Pogram output

a Adenine

In PHP 4 all variables were copied by value when the = operator is used. However, in PHP 5 objects are copied by reference. This means that if the copy is changed the original is changed because actually the copy is a pointer to the original. In PHP 4 you need to use the =& to copy by reference. The clone keyword can be used to make a copy of an object in PHP 5.


Previous  Contents  Next
References

Google

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

© 2006-2007 Alex Amies