Jump to content
Larry Ullman's Book Forums

Prometheus Abstract Class Example


Recommended Posts

I got some idea after reading the abstract classes example in the book that i would create a Life abstract class and extend it to other life forms. I extended Humans and Alien Mothers from Prometheus as they were genetically engineered by them. But it seems that life as it is comes with the same attributes as from the base abstract class has been inherited by the humans even though Prometheus may have not wanted them to become so smart. So my guess is that is why Prometheus wanted to destroy the human race as they got a bit too smart!! ^_^

 

There was something that i wanted to do in the Prometheus class below, well in the createHuman() method it would of been nice to have been able to create new classes of Human when this method was run but as far as i know Inner PHP classes are not possible in PHP but are in Java. This also made me think of object recursion as we do have function recursion when a function calls itself.

 

Well i hope you like this fun example, i thought it would be simple to start of with but it started getting complicated, i have also realized that some of the methods should of been better prepared up the tree as in the human class they are inheriting abilities that should only of been possessed by Prometheus. So even though i made some mistakes below i have learned something from this example.

 

<?php
// A generic base abstract class for the existance of life. An abstract class can't be instantiated but can be extended.
// Some of you might argue that the abstract class for this situation would be God but i wanted to keep things simple.
abstract class Life {
protected $name;
protected $lifeFormType;
protected $occupation;
abstract public function eat();
abstract public function rest();
abstract public function learn();
abstract public function create();
abstract public function play();
abstract public function getLifeFormStats();
}
// I am assuming in this example that Prometheus is the first type of life form in our universe
class Prometheus extends Life {

private static $_prometheus_life_counter = 0;

// Constructor:
   function __construct($name, $occupation) {
    // Store the life form statistics
    $this->name = $name;
    $this->occupation = $occupation;
 $this->lifeFormType = get_class();
 self::$_prometheus_life_counter++;
   } // End of constructor.

public function getLifeFormStats() {
 // Build a string of life form stats to be returned by this function
 echo "The " . $this->lifeFormType . "'s name is " . $this->name . " its occupation is to " . $this->occupation . ".";
}

public function eat() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is enjoying eating his Granite Crunches.";
}

public function learn() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is learning how to build new space ships and create intelligent life forms.";
}

public function create() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is creating a new space ship.";
}

public function createHuman() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is creating a new human clone";
}

public function createAlienMother() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is creating a new Alien Mother to lay more Face Hugger Eggs";
}

public function play() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is playing the game \"Holographic Lazer Rock\" on his new iHolo4p";
}

public function getTotalPrometheusLifeForms () {
 return self::$_prometheus_life_counter;
}

function __destruct() {
 self::$_prometheus_life_counter--;
}
}
class Human extends Prometheus {
private static $_human_life_counter = 0;

function __construct($name, $occupation) {
    // Store the life form statistics
    $this->name = $name;
    $this->occupation = $occupation;
 $this->lifeFormType = get_class();
 self::$_human_life_counter++;
   } // End of constructor.

public function eat() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is eating.";
}

public function learn() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is learning.";
}

public function create() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is creating houses, roads and schools";
}

public function createClone() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is cloning sheep, humans and other animals";
}

public function play() {
 echo "A " . $this->lifeFormType . " named " . $this->name . " is playing the game \"Holographic Lazer Rock\" on his new iHolo4p";
}

public function getTotalHumanLifeForms () {
 return self::$_human_life_counter;
}

function __destruct() {
 self::$_human_life_counter--;
}
}
class AlienMother extends Prometheus {
private static $_alien_mother_life_counter = 0;

function __construct($name, $occupation) {
    // Store the life form statistics
    $this->name = $name;
    $this->occupation = $occupation;
 $this->lifeFormType = get_class();
 self::$_alien_mother_life_counter++;
   } // End of constructor.

// I will not override or add new methods here as this is for example purposes only.

public function getTotalAlienMotherLifeForms () {
 return self::$_alien_mother_life_counter;
}

function __destruct() {
 self::$_alien_mother_life_counter--;
}
}
class FaceHugger extends AlienMother {

private static $_face_hugger_life_counter = 0;

function __construct($name, $occupation) {
    // Store the life form statistics
    $this->name = $name;
    $this->occupation = $occupation;
 $this->lifeFormType = get_class();
 self::$_face_hugger_life_counter++;
   } // End of constructor.

// I will not override or add new methods here as this is for example purposes only.

public function getTotalAlienMotherLifeForms () {
 return self::$_face_hugger_life_counter;
}

function __destruct() {
 self::$_face_hugger_life_counter--;
}
}

Link to comment
Share on other sites

Yeah i know i should plan out for real classes, its just i had limited time earlier so i just came up with the idea and just tried to see what i could hack together in the time i had. Yeah those abstract methods in life class i can see are not correct those could of been added to the child classes. The whole code actually needs some good modular separation but it was just for fun and so we can discuss.

 

So for our real sites we can have something like a generic abstract user class then possibly extend to make a member class and extend also to make another admin class.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...