here is one possible (but useless) use of an Abstract Class in php
each type that derives from AbstractCreature are sure to have some basic variables and some basic functions...
for each subclass.. these functions must act diferently but stay named the same
this is for some reason a lot of code... mostly because i was bored when i wrote it..
class AbstractCreature
{
var $creature_id;
var $likes_to_eat;
var $hungry;
function AbstractCreature()
{
$this->creature_id = NULL;
$this->likes_to_eat = NULL;
$this->hungry = FALSE;
}
function eat ()
{
/// left empty: to be filled by sub-class
}
function consume ( $item )
{
if ( $this->hingry == TRUE ) {
$this->hungry = FALSE;
}
}
functoin isEddible ( $item )
{
//// subclass this function as it is creture specific
return FALSE;
}
}
class Herbivore extends AbstractCreature
{
function Herbivore ()
{
$this->AbstractCreature();
$this->creature_id = 'Herbivore'
$this->likes_to_eat = 'plants';
}
function eat ()
{
$nearest_plant = $this->findNearestPlant();
if ( $this->isEddible($nearest_plant) ) {
consume( $nearest_plant );
}
}
function findNearestPlant()
{
/// this happens magically
/// $found should be an AbstractCreature
/// $found should contain a creatue_id identifying it as a 'plant'
return $found->creature_id;
}
function isEddible ( $item )
{
if ( $item == 'plant' ) {
return TRUE;
} else {
return FALSE;
}
}
}
class Carnivore extends AbstractCreature
{
function Carnivore ()
{
$this->AbstractCreature();
$this->creature_id = 'Carnivore'
$this->likes_to_eat = 'Herbivore';
}
function eat ()
{
$nearest_herbivore = $this->findNearestHerbivore();
if ( $this->isEddible($nearest_herbivore) ) {
consume( $nearest_herbivore );
}
}
function findNearestHerbivore()
{
/// this happens
/// $found should be an AbstractCreature
/// $found should contain a creatue_id identifying it as a 'Herbivore'
return $found->creature_id;
}
function isEddible ( $item )
{
if ( $item == 'Herbivore' ) {
return TRUE;
} else {
return FALSE;
}
}
}