Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Dragonball Class
- *
- * Example of a class that prints a message once 7 calls to iFoundBall method
- * @todo Persist object between calls using serialization, database, or sessions
- * @author Eric LInberg <elinberg@gmail.com
- */
- class Dragonball {
- const MESSAGE = 'You can ask your wish'."\n";
- var $ballCount = 0;
- var $maxBallCount = 0;
- public function __construct( $ballCount, $maxBallCount){
- if( !is_integer($ballCount) ){
- throw new \Exception('$ballCount must be of type integer');
- }
- if( !is_integer($maxBallCount) ){
- throw new \Exception('$maxBallCount must be of type integer');
- }
- $this->setBallCount($ballCount);
- $this->setMaxBallCount($maxBallCount);
- }
- private function setMaxBallCount( $count){
- $this->maxBallCount = $count;
- }
- public function iFoundBall(){
- $this->getBallCount();
- }
- private function getCount(){
- return $this->ballCount;
- }
- public function getMaxBallCount(){
- return $this->maxBallCount;
- }
- private function getBallCount(){
- $currentCount = $this->getCount();
- if($currentCount > -1 && $currentCount < 7){
- $this->incrementBallCount();
- return $currentCount;
- } else if( $currentCount == 7 ){
- $this->setBallCount(0);
- echo $this->printMessage();
- }
- }
- private function printMessage(){
- echo self::MESSAGE;
- }
- private function setBallCount( $count){
- $this->ballCount = $count;
- }
- private function incrementBallCount(){
- $this->ballCount++;
- }
- }
- $ballMin = 0;
- $ballMax = 7;
- $dragon = new Dragonball($ballMin, $ballMax);
- $maxIterations = 17;
- for( $i = 0; $i < $maxIterations; $i++) {
- $dragon->iFoundBall();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement