Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language PHP5

(Objects, phpdoc comments, exceptions)

Date:09/22/05
Author:Dave Marshall
URL:n/a
Comments:9
Info:http://www.php.net
Score: (3.02 in 774 votes)
<?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

VersionAuthorDateCommentsRate
another versionKenneth Clark05/17/054
PHP5 code using classesTim M09/02/059

Comments

>>  Eric J K said on 02/14/07 14:08:57

Eric J K Beautifull

>>  gurubob said on 02/10/08 08:02:34

gurubob Ok, so this is nice - it demonstrates advanced PHP features, but why oh why can't someone do a *simple* version like some of the other languages?

<?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";
?>

>>  Devlin said on 07/10/08 01:57:49

Devlin In all honesty, this is just OOP for the sake of OOP and excessive to say the least.

>>  Liz said on 07/30/08 08:45:36

Liz Eurgh, what on Earth is this? Like what Devlin said, it's OOP for the sake of OOP. Please, post something that'd actually be used in real life.

>>  cwi said on 09/29/08 17:22:23

cwi @the writers above:
What do you mean?
It's really a self explaining and good OOP-example.

>>  Brian May said on 12/17/08 16:15:25

Brian May Way to fail at using @since.

>>  Taras said on 05/27/09 10:49:45

Taras If I'd like to see the usability of php, I'd be scared with such an example... :)
Gurubob gives much friendlier code.

>>  Marc said on 07/12/09 20:08:45

Marc Yes, Gurubobs is on this page (by far) the best code for 99 bottles.
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! ;-)

>>  JuannyCinco said on 11/02/09 17:38:09

JuannyCinco It's a bad example of OOP. Exceptions are for exceptions and NOT to control flow.

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!

Name:

eMail:

URL:

Security Code:
  
Comment: