Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Ifrah Shahid
lab-exercises
Commits
6a503821
Commit
6a503821
authored
Feb 10, 2016
by
danmcquillan
Browse files
adding simple classes
parent
ee14f05e
Changes
9
Hide whitespace changes
Inline
Side-by-side
week-15/simple-classes/Rectangle.php
0 → 100644
View file @
6a503821
<?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
week-15/simple-classes/Rectangle1.php
0 → 100644
View file @
6a503821
<?
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.
week-15/simple-classes/Rectangle2.php
0 → 100644
View file @
6a503821
<?
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.
week-15/simple-classes/pets1.php
0 → 100644
View file @
6a503821
<?
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
);
?>
week-15/simple-classes/pets2.php
0 → 100644
View file @
6a503821
<?
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
);
?>
week-15/simple-classes/pets3.php
0 → 100644
View file @
6a503821
<!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
week-15/simple-classes/pets4.php
0 → 100644
View file @
6a503821
<!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>
week-15/simple-classes/rectangle1.php
0 → 100644
View file @
6a503821
<?
// 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
);
?>
week-15/simple-classes/rectangle2.php
0 → 100644
View file @
6a503821
<?
// 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
);
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment