Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Created October 3, 2012 05:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesleybliss/3825115 to your computer and use it in GitHub Desktop.
Save wesleybliss/3825115 to your computer and use it in GitHub Desktop.
Basic PHP Class Example
<?php
// Only allow this script to be run via the command line
if ( strtoupper(PHP_SAPI) !== 'CLI' ) {
header( 'Content-type: text/plain' );
echo 'This script can only be run via the command line.',
PHP_EOL, 'Usage: php -f ',
array_pop(explode('/', $_SERVER['SCRIPT_NAME']));
exit( 1 );
}
interface IMamal {
public function eat( $food );
public function sleep( $hours );
}
abstract class Human implements IMamal {
public $name, $age,
$eyeColor, $hairColor;
public $energy = 0;
abstract function eat( $food );
abstract function sleep( $hours );
}
class Person extends Human {
private $foodEmotions = array(
'delicious', 'tasty', 'scrumptious',
'savory', 'sweet', 'yummy', 'mouthwatering'
);
private $sleepBad = array( 'meager', 'paltry', 'pathetic' );
private $sleepGood = array( 'healthy', 'somberous', 'wonderful', 'rejuvenating' );
function __construct( $name, $age, $eyeColor, $hairColor ) {
$this->name = $name;
$this->age = $age;
$this->eyeColor = $eyeColor;
$this->hairColor = $hairColor;
}
public function eat( $food ) {
if ( $this->energy >= 100 ) {
echo 'Energy is already full. I can\'t eat anymore!';
return;
}
$this->energy += rand( 10, 30 );
echo PHP_EOL, sprintf( '%s just ate some %s %s! He is now %s%% rested.', $this->name,
$this->foodEmotions[rand(0, (count($this->foodEmotions) - 1))], $food, $this->energy
);
}
public function sleep( $hours = 1 ) {
if ( $this->energy >= 100 ) {
echo 'Too much sleep is bad, get back to work!';
return;
}
$this->energy += $hours;
if ( $hours <= 4 ) {
$sleepType = $this->sleepBad[rand(0, (count($this->sleepBad) - 1))];
}
else {
$sleepType = $this->sleepGood[rand(0, (count($this->sleepGood) - 1))];
}
echo PHP_EOL, sprintf( '%s slept a %s %s hours, and is now %s%% rested.',
$this->name, $sleepType, $hours, $this->energy
);
}
public function work( $hours ) {
if ( $this->energy <= 0 ) {
echo PHP_EOL, $this->name, ' is overworked and needs sleep!';
return;
}
$this->energy -= $hours;
echo PHP_EOL, $this->name, ' worked ', $hours, ' hours.';
}
public function isAsleep() {
return ( $this->energy <= 10 );
}
public function info() {
return sprintf(
'%s is a %s year old %s, and has %s eyes and %s hair.',
$this->name, $this->age, get_class($this), $this->eyeColor, $this->hairColor
);
}
}
$me = new Person( 'Wes', 28, 'brown', 'brown' );
echo PHP_EOL, $me->info(), PHP_EOL;
$me->eat( 'pizza' );
$me->sleep( rand(1, 10) );
$me->eat( 'lasagna' );
$me->eat( 'falafel' );
for ( $i = 0; $i < 9; $i++ ) {
$me->work( rand(8, 12) );
}
echo PHP_EOL, $me->name, (
$me->isAsleep() ? ' is now sleeping!' : ' is wide awake.'
);
echo PHP_EOL;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment