rendered paste body<?php
class Bird
{
public static $type = 'bird';
public static function fly($direction = 'around')
{
printf("<p>The bird is flying %s.</p>\n", $direction);
}
private $name;
private $breed;
private $price;
function __construct($name = 'Anonymous', $breed = 'Unknown', $price = 100)
{
$this->name = $name;
$this->breed = $breed;
$this->price = $price;
}
function setPrice($price)
{
$this->price = $price < 0 ? 0 : $price;
}
function setName($name)
{
$this->name = $name;
}
function setBreed($breed)
{
$this->breed = $breed;
}
function getName()
{
return $this->name;
}
function getPrice()
{
return $this->price;
}
function getBreed()
{
return $this->breed;
}
public function display()
{
printf("<p>The %s named '%s' is a %s and costs \$%.2f.</p>\n",
self::$type, $this->name, $this->breed, $this->price);
}
}
$aBird = new Bird();
$tweety = new Bird('Tweety', 'Canary');
$tweety->setPrice(24.95);
$polly = new Bird('Polynesia', 'Parrot');
$polly->setPrice(54.95);
$polly->display();
$tweety->display();
$aBird->display();
$magpie = new Bird('Malaysia', 'Magpie', 7.5);
$magpie->display();
$magpie->setPrice(-14.95);
$magpie->display();
Bird::fly();
Bird::fly('South');
$sam = new Bird('Toucan Sam', 'toucan');
$sam->display();
class Employee
{
const CATEGORY_WORKER = 0;
const CATEGORY_SUPERVISOR = 1;
const CATEGORY_MANAGER = 2;
public static $jobTitles = array('regular worker', 'supervisor', 'manager');
public static $payRates = array(5, 8.25, 17.5);
public static function getCategoryInfo($cat)
{
printf("<p>A %s makes \$%.2f per hour.</p>\n",
self::$jobTitles[$cat],
self::$payRates[$cat]);
}
public static function calcGrossPay($hours, $cat)
{
return $hours * self::$payRates[$cat];
}
private $firstName;
private $lastName;
private $id;
private $category;
public function __construct($fname, $lname, $id, $cat = self::CATEGORY_WORKER)
{
$this->firstName = $fname;
$this->lastName = $lname;
$this->id = $id;
$this->category = $cat;
}
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->firstName;
}
public function getId()
{
return $this->id;
}
public function getCategory()
{
return $this->category;
}
public function setFirstName($fname)
{
$this->firstName = $fname;
}
public function setLastName($lname)
{
$this->lastName = $lname;
}
public function setId($id)
{
$this->id = $id;
}
public function promote()
{
if($this->category < self::CATEGORY_MANAGER)
$this->category++;
}
public function demote()
{
if($this->category > self::CATEGORY_WORKER)
$this->category--;
}
public function display()
{
printf("<p>%s %s is Employee #%d, and is a %s making \$%.2f per hour.</p>\n",
$this->getFirstName(),
$this->getLastName(),
$this->getId(),
self::$jobTitles[$this->getCategory()],
self::$payRates[$this->getCategory()]);
}
}
Employee::getCategoryInfo(Employee::CATEGORY_SUPERVISOR);
$bob = new Employee('Bob', 'Smith', 102, Employee::CATEGORY_SUPERVISOR);
$bob->display();
$bob->promote();
$bob->display();
$bob->promote();
$bob->display();
$bob->demote();
$bob->display();
$hours_worked = 35.5;
printf("<p>If %s %s works %.2f hours, he will gross \$%.2f.</p>\n",
$bob->getFirstName(),
$bob->getLastName(),
$hours_worked,
Employee::calcGrossPay($hours_worked, $bob->getCategory()));
class Parrot extends Bird
{
public function __construct($name)
{
parent::__construct($name, 'parrot', 25);
}
public function birdCall()
{
printf("<p>%s says: *squawk*</p>\n", $this->getName());
}
}
$birdie = new Parrot('Birdie');
$birdie->birdCall();
?>