How to implement a class named dragonball. This class must have an attribute named ballcount (which starts from 0) and a method ifoundaball. When ifoundaball is called, ballcount is increased by one. If the value of ballcount is equal to seven, then the message you can ask your wish is printed, and ballcount is reset to 0. How would you implement this class?
Answer Posted / testme
<?php
class Dragonball {
private $ballCount = 0; // starts from 0
public function iFoundaBall() {
$this->ballCount++;
if ($this->ballCount === 7) {
echo "You can ask your wish
";
$this->ballCount = 0; // reset to 0
}
}
}
// Example usage:
$goku = new Dragonball();
for ($i = 0; $i < 10; $i++) {
$goku->iFoundaBall();
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Can we run php on tomcat server?
What is php and its features?
Why laravel is best php framework?
How can I load data from a text file into a table?
What is the difference between single quoted string and double quoted string?
How to convert a json string to an array in php?
What is $_ request in php?
Explain about require and include function?
How to test if a variable is an array?
Tell me how to create a session? How to set a value in session? How to remove data from a session?
Should I use mysqli or pdo?
What is a variable cost example?
Which is not a php magic constant?
What are interfaces in php?
What does the unlink() function mean?