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
What is the difference between the functions strstr() and stristr()?
Which is better php or wordpress?
What does $globals means?
Do you know how to enable error reporting in php?
How to check whether a number is prime or not?
How can I embed a java program in php file and what changes have to be done in php.ini file?
Who is the father of php?
What is a session in php?
Is php 7 backwards compatible?
Why does php need server?
What is the latest version of php?
How is session data stored?
How to change the principal password?
Is polymorphism inherited?
What is T_PAAMAYIM_NEKUDOTAYIM?