-->
saved as testing.php<?php //Define an interface that will allow conversion of objecs to strings
interface conversionOptions{
public function asString();
public function asHTML();
}
//Define a 'cat' class that implements the interface
class cat implements conversionOptions{
//Properties of a cat
public $name;
public $color;
//Constructor that requires a name
public function __construct($namesake){
$this->name=$namesake;
}
//implement the strung function of the interface
public function asString(){
return "Cat - Name:{$this->name},Color:{$this->color}";
}
//implement the HTML function of the interface
public function asHTML(){
return "<ul><li>Cat</li><ul><li>Name:{$this->name}</li>
<li>Color:{$this->color}</li></ul></ul>";
}
}
//Define class that implements the interface,for acronym definitions
class acronym implements conversionOptions{
//Properties of an acronym
private $name;
private $definition;
//Constructor that requires the term entry and definition
public function __construct($entry,$def){
$this->name=$entry;
$this->definition=$def;
}
//Implement the string function of the interface
public function asString(){
return "{$this->name}:{$this->definition}";
}
//Implementthe HTML function of the interface
public function asHTML(){
return "<dl><dt>{$this->name}</dt><dd>{$this->definition}</dd></dl>";
}
}
//Instatiate a cat & gie it data
$my_cat=new cat('Wiley');
$my_cat->color='grey tabby';
//Now return the cat as a string &echo it:
$catstring=$my_cat->asString();
echo "<pre>{$catstring}</pre>\n";
//and do the same as HTML
echo $my_cat->asHTML();
//NOW construct an acronym definition
$my_anac=new acronym('SCA','Society for Creative Anacronism');
//Covert it into string form:
$anac=$my_anac->asString();
echo "<pre>{$anac}</pre>\n";
//Now output as HTML:
echo $my_anac->asHTML();
?>
Output
Cat - Name:Wiley,Color:grey tabby- Cat
- Name:Wiley
- Color:grey tabby
SCA:Society for Creative Anacronism- SCA
- Society for Creative Anacronism
Download for this tutorial
No comments:
Post a Comment