Question
MODIFY THE FOLLOWING PHP SCRIPT AS FOLLOWS: 1. Add an additional property called type to the Pet class 2. Set all data fields to private
MODIFY THE FOLLOWING PHP SCRIPT AS FOLLOWS:
1. Add an additional property called "type" to the Pet class
2. Set all data fields to "private"
3. Provide setter and getter methods for these private data fields
4. Create a new method called "talk" in the Pet class to display the message like this: Bucky is talking. //Bucky is the actual name of pet
5. override the "talk" method in both the Cat and Dog classes to say: Bucky meows. //for a cat Snoopy barks. //for a dog
6. Create and modify all constructors accordingly to initialize the data fields
7. Add the additional codes to display the cat and dog types.
8. Add the additional code to have the pets talk.
1
2
3
4
5
6
7
8
9
10 // This page defines and uses the Pet, Cat, and Dog classes.
11
12 # ***** CLASSES ***** #
13
14 /* Class Pet.
15 * The class contains one attribute: name.
16 * The class contains four methods:
17 * - _ _construct()
18 * - eat()
19 * - sleep()
20 * - play()
21 */
22 class Pet {
23 public $name;
24 function _ _construct($pet_name) {
25 $this->name = $pet_name;
26 }
27 function eat() {
28 echo "
$this->name is eating.
";29 }
30 function sleep() {
31 echo "
$this->name is sleeping.
";32 }
33
34 // Pets can play:
35 function play() {
36 echo " $this->name is playing.
37 }
38
39 } // End of Pet class.
40
41 /* Cat class extends Pet.
42 * Cat overrides play().
43 */
44 class Cat extends Pet {
45 function play() {
46 echo "
$this->name is climbing.
";47 }
48 } // End of Cat class.
49
50 /* Dog class extends Pet.
51 * Dog overrides play().
52 */
53 class Dog extends Pet {
54 function play() {
55 echo "
$this->name is fetching.
";56 }
57 } // End of Dog class.
58
59 # ***** END OF CLASSES ***** #
60
61 // Create a dog:
62 $dog = new Dog('Satchel');
63
64 // Create a cat:
65 $cat = new Cat('Bucky');
66
67 // Create an unknown type of pet:
68 $pet = new Pet('Rob');
69
70 // Feed them:
71 $dog->eat();
72 $cat->eat();
73 $pet->eat();
74
75 // Nap time:
76 $dog->sleep();
77 $cat->sleep();
78 $pet->sleep();
79
80 // Have them play:
81 $dog->play();
82 $cat->play();
83 $pet->play();
84
85 // Delete the objects:
86 unset($dog, $cat, $pet);
87
88 ?>
89
90
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started