Language PHP5
(Objects, phpdoc comments, exceptions)
| Date: | 09/22/05 |
| Author: | Dave Marshall |
| URL: | n/a |
| Comments: | 9 |
| Info: | http://www.php.net |
| Score: |
<?php
/**
* One bottle left exception
*
* @author Dave Marshal
* @since 22/09/2005
* @package Bottles
*/
class LastBottleException extends Exception {}
/**
* Out of bottles exception
*
* @author Dave Marshal
* @since 22/09/2005
* @package Bottles
*/
class OutOfBottlesException extends Exception {}
/**
* Wall Class
*
* @author Dave Marshal
* @since 22/09/2005
* @package Bottles
*/
class Wall {
/**
* Collection of Bottles
*/
private $bottle = array();
/**
* Wall Constructor
*
* @param int $numberOfBottles The number of bottles on this wall
*/
public function __construct($numberOfBottles=99)
{
$this->addBottles($numberOfBottles);
}
/**
* Wall Desctructor
*/
public function __Destruct() {}
/**
* Get next bottle
*
* @param boolean $force Force the function to return the last bottle
* @returns Bottle
* @throws OutOfBeerException
*/
public function getNextBottle($force = FALSE)
{
if (count($this->bottle) > 1) return array_pop($this->bottle);
else if (count($this->bottle)==1)
{
if ($force) return array_pop($this->bottle);
else throw new LastBottleException();
}
else throw new OutOfBottlesException();
}
/**
* Get number of beers left on the wall
*
* @return int
*/
public function getNumberOfBottles()
{
return count($this->bottle);
}
/**
* Add more bottles
*
* @param int $numberOfBottles
*/
public function addBottles($numberOfBottles)
{
for ($i=0;$i<$numberOfBottles;$i++)
{
$this->bottle[] = new Bottle();
}
}
}
/**
* Bottle Class
*
* @author Dave Marshal
* @since 22/09/2005
* @package Bottles
*/
class Bottle {
/**
* Bottle Constructor
*/
public function __construct() {}
/**
* Bottle Destructor
*/
public function __destruct() {}
}
$wall = new Wall(99);
$continue = TRUE;
while ($continue)
{
try {
$bottlesLeftBefore = $wall->getNumberOfBottles();
$bottle = $wall->getNextBottle();
$bottlesLeftAfter = $wall->getNumberOfBottles();
echo $bottlesLeftBefore." bottles of beer on the wall, ".$bottlesLeftBefore." bottles of
beer.\n";
echo "Take one down and pass it around, ".$bottlesLeftAfter." bottles of beer on the wall.\n\n";
}
catch (LastBottleException $oneLeft)
{
$bottle = $wall->getNextBottle(TRUE);
echo "1 bottle of beer on the wall, 1 bottle of beer.\n";
echo "Take one down and pass it around, no more bottles of beer on the wall.\n\n";
}
catch (OutOfBottlesException $out)
{
echo "No more bottles of beer on the wall, no more bottles of beer.\n";
echo "Go to the store and buy some more, ";
$wall->addBottles(99);
echo $wall->getNumberOfBottles()." bottles of beer on the wall.\n\n";
$continue=FALSE;
}
}
?>
Download Source | Write Comment
Alternative Versions
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| another version | Kenneth Clark | 05/17/05 | 4 | |
| PHP5 code using classes | Tim M | 09/02/05 | 9 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
<?php
for ($c=99; $c>1; $c--)
echo "$c bottles of beer on the wall, $c bottles of beer, take one down and pass it around, ".($c-1)." bottles of beer on the wall!\n";
echo "1 bottle of beer on the wall, one bottle of beer, take one down and pass it around, no more bottles of beer on the wall!\n";
?>
What do you mean?
It's really a self explaining and good OOP-example.
Gurubob gives much friendlier code.
but he could improve it: 1) "1 bottle" 2) add the chorus for 0 bottles.
When I saw Daves example, I was thinking: Damn, never PHP5!