Language PHP5
(PHP5 code using classes)
Date: | 09/02/05 |
Author: | Tim M |
URL: | n/a |
Comments: | 9 |
Info: | n/a |
Score: | ![]() |
<?php class Bottles{ private $Amount = 0; private $Original = 0; final function __construct($Amount){ $this->Original = $Amount; $this->Amount = $Amount; } final public function PassAround(){ $Bottles = &$this->Amount; if ($Bottles == 0){ return $this->BuySomeMore(); }else if ($Bottles == 1){ $BottlesString = "1 bottle"; }else{ $BottlesString = "$Bottles bottles"; } $Data = "$BottlesString of beer on the wall, $BottlesString of beer.\r\n"; $Bottles--; if ($Bottles == 0){ $BottlesString = "no more bottles"; }elseif ($Bottles == 1){ $BottlesString = "1 bottle"; }else{ $BottlesString = "$Bottles bottles"; } $Data .= "Take one down and pass it around, $BottlesString of beer on the wall.\r\n"; return $Data; } final public function BuySomeMore(){ # $Bottles = $this->Original; $Data = "No more bottle of beer on the wall, no more bottles of beer.\r\n"; $Data .= "Go to the store and buy some more, $Bottles bottles of beer on the wall.\r\n"; return $Data; } } class PlayBeerWallSong{ private $Bottles = null; private $Amount = 0; final function __construct($Amount){ # $this->Bottles = new Bottles($Amount); $this->Amount = $Amount; } final public function Play(){ $this->Stream(false); } final public function Stream($doStream = true){ # for ($I=0;$I<=$this->Amount;$I++){ # if ($doStream){ echo $this->Bottles->PassAround(); }else{ $Return .= $this->Bottles->PassAround(); } } if ($doStream){ return true; }else{ return $Return; } } } $Song = new PlayBeerWallSong(99); //$Song->Play(); # Return the song as a string $Song->Stream(); # Echo the song ?>
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
Objects, phpdoc comments, exceptions | Dave Marshall | 09/22/05 | 9 | ![]() ![]() |
another version | Kenneth Clark | 05/17/05 | 4 | ![]() ![]() |
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
The '\r\n' did not come through, so no new lines, just a block of script.
(could be a setting on my machine)
TH
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/content/p/d/e/pdemarco/html/bottles.php on line 3
Sorry for the noise.