Commit 6a503821 authored by danmcquillan's avatar danmcquillan
Browse files

adding simple classes

parent ee14f05e
<?php # Script 4.5 - Rectangle.php
/* This page defines the Rectangle class.
* The class contains two attributes: width and height.
* The class contains five methods:
* - __construct()
* - setSize()
* - getArea()
* - getPermeter()
* - isSquare()
*/
class Rectangle {
// Declare the attributes:
public $width = 0;
public $height = 0;
// Constructor:
function __construct($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
}
// Method to set the dimensions:
function setSize($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
}
// Method to calculate and return the area:
function getArea() {
return ($this->width * $this->height);
}
// Method to calculate and return the perimeter:
function getPerimeter() {
return ( ($this->width + $this->height) * 2 );
}
// Method to determine if the rectange
// is also a square.
function isSquare() {
if ($this->width == $this->height) {
return true; // Square
} else {
return false; // Not a square
}
}
} // End of Rectangle class.
\ No newline at end of file
<?
class Rectangle {
// Declare the attributes:
public $width = 0;
public $height = 0;
// Method to set the dimensions:
function setSize($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
}
// Method to calculate and return the area:
function getArea() {
return ($this->width * $this->height);
}
// Method to calculate and return the perimeter:
function getPerimeter() {
return ( ($this->width + $this->height) * 2 );
}
// Method to determine if the rectange
// is also a square.
function isSquare() {
if ($this->width == $this->height) {
return true; // Square
} else {
return false; // Not a square
}
}
} // End of Rectangle class.
<?
class Rectangle {
// Declare the attributes:
public $width = 0;
public $height = 0;
// Constructor:
function __construct($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
}
// Method to set the dimensions:
function setSize($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
}
// Method to calculate and return the area:
function getArea() {
return ($this->width * $this->height);
}
// Method to calculate and return the perimeter:
function getPerimeter() {
return ( ($this->width + $this->height) * 2 );
}
// Method to determine if the rectange
// is also a square.
function isSquare() {
if ($this->width == $this->height) {
return true; // Square
} else {
return false; // Not a square
}
}
} // End of Rectangle class.
<?
class Pet {
// Declare the attributes:
public $name;
// Constructor assigns the pet's name:
function __construct($pet_name) {
$this->name = $pet_name;
}
// Pets can eat:
function eat() {
echo "<p>$this->name is eating.</p>";
}
// Pets can sleep:
function sleep() {
echo "<p>$this->name is sleeping.</p>";
}
} // End of Pet class.
/* Cat class extends Pet.
* Cat has additional method: climb().
*/
class Cat extends Pet {
function climb() {
echo "<p>$this->name is climbing.</p>";
}
} // End of Cat class.
/* Dog class extends Pet.
* Dog has additional method: fetch().
*/
class Dog extends Pet {
function fetch() {
echo "<p>$this->name is fetching.</p>";
}
} // End of Dog class.
# ***** END OF CLASSES ***** #
// Create a dog:
$dog = new Dog('Rover');
// Create a cat:
$cat = new Cat('Whiskers');
// Feed them:
$dog->eat();
$cat->eat();
// Nap time:
$dog->sleep();
$cat->sleep();
// Do animal-specific thing:
$dog->fetch();
$cat->climb();
// Delete the objects:
unset($dog, $cat);
?>
<?
class Pet {
public $name;
function __construct($pet_name) {
$this->name = $pet_name;
}
function eat() {
echo "<p>$this->name is eating.</p>";
}
function sleep() {
echo "<p>$this->name is sleeping.</p>";
}
// Pets can play:
function play() {
echo "<p>$this->name is playing.</p>";
}
} // End of Pet class.
/* Cat class extends Pet.
* Cat overrides play().
*/
class Cat extends Pet {
function play() {
echo "<p>$this->name is climbing.</p>";
}
} // End of Cat class.
/* Dog class extends Pet.
* Dog overrides play().
*/
class Dog extends Pet {
function play() {
echo "<p>$this->name is fetching.</p>";
}
} // End of Dog class.
# ***** END OF CLASSES ***** #
// Create a dog:
$dog = new Dog('Rover');
// Create a cat:
$cat = new Cat('Whiskers');
// Create an unknown type of pet:
$pet = new Pet('Rob');
// Feed them:
$dog->eat();
$cat->eat();
$pet->eat();
// Nap time:
$dog->sleep();
$cat->sleep();
$pet->sleep();
// Have them play:
$dog->play();
$cat->play();
$pet->play();
// Delete the objects:
unset($dog, $cat, $pet);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pets</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 5.5 - pets3.php
// This page defines and uses the Pet, Cat, and Dog classes.
# ***** CLASSES ***** #
/* Class Pet.
* The class contains one attribute: name.
* The class contains four methods:
* - __construct()
* - eat()
* - sleep()
* - play()
*/
class Pet {
public $name;
function __construct($pet_name) {
$this->name = $pet_name;
self::sleep();
}
function eat() {
echo "<p>$this->name is eating.</p>";
}
function sleep() {
echo "<p>$this->name is sleeping.</p>";
}
function play() {
echo "<p>$this->name is playing.</p>";
}
} // End of Pet class.
/* Cat class extends Pet.
* Cat overrides play().
*/
class Cat extends Pet {
function play() {
// Call the Pet::play() method:
parent::play();
echo "<p>$this->name is climbing.</p>";
}
} // End of Cat class.
/* Dog class extends Pet.
* Dog overrides play().
*/
class Dog extends Pet {
function play() {
// Call the Pet::play() method:
parent::play();
echo "<p>$this->name is fetching.</p>";
}
} // End of Dog class.
# ***** END OF CLASSES ***** #
// Create a dog:
$dog = new Dog('Satchel');
// Create a cat:
$cat = new Cat('Bucky');
// Create an unknown type of pet:
$pet = new Pet('Rob');
// Feed them:
$dog->eat();
$cat->eat();
$pet->eat();
// Nap time:
$dog->sleep();
$cat->sleep();
$pet->sleep();
// Have them play:
$dog->play();
$cat->play();
$pet->play();
// Delete the objects:
unset($dog, $cat, $pet);
?>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pets</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php # Script 5.5 - pets3.php
// This page defines and uses the Pet, Cat, and Dog classes.
spl_autoload_register(function ($class_name) {
$class_name = strtolower($class_name);
include $class_name . '.class.php';
});
// Create a dog:
$dog = new Dog('Satchel');
// Create a cat:
$cat = new Cat('Bucky');
// Create an unknown type of pet:
$pet = new Pet('Rob');
// Feed them:
$dog->eat();
$cat->eat();
$pet->eat();
// Nap time:
$dog->sleep();
$cat->sleep();
$pet->sleep();
// Have them play:
$dog->play();
$cat->play();
$pet->play();
// Delete the objects:
unset($dog, $cat, $pet);
?>
</body>
</html>
<?
// Include the class definition:
require('Rectangle1.php');
// Define the necessary variables:
$width = 42;
$height = 7;
// Print a little introduction:
echo "<h2>With a width of $width and a height of $height...</h2>";
// Create a new object:
$r = new Rectangle();
// Assign the rectangle dimensions:
$r->setSize($width, $height);
// Print the area:
echo '<p>The area of the rectangle is ' . $r->getArea() . '</p>';
// Print the perimeter:
echo '<p>The perimeter of the rectangle is ' . $r->getPerimeter() . '</p>';
// Is this a square?
echo '<p>This rectangle is ';
if ($r->isSquare()) {
echo 'also';
} else {
echo 'not';
}
echo ' a square.</p>';
// Delete the object:
unset($r);
?>
<?
// Include the class definition:
require('Rectangle2.php');
// Define the necessary variables:
$width = 160;
$height = 75;
// Print a little introduction:
echo "<h2>With a width of $width and a height of $height...</h2>";
// Create a new object:
$r = new Rectangle($width, $height);
// Print the area.
echo '<p>The area of the rectangle is ' . $r->getArea() . '</p>';
// Print the perimeter.
echo '<p>The perimeter of the rectangle is ' . $r->getPerimeter() . '</p>';
// Is this a square?
echo '<p>This rectangle is ';
if ($r->isSquare()) {
echo 'also';
} else {
echo 'not';
}
echo ' a square.</p>';
// Delete the object:
unset($r);
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment