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

(another version)

Date:05/17/05
Author:Kenneth Clark
URL:n/a
Comments:4
Info:n/a
Score: (2.68 in 19 votes)
class SingBeerWallSong{
    private $noBeers;
    private $output;
    private $outputType;
    public function __construct($noBeers, $type){
        $this->noBeers  = $noBeers;
        $this->type     = $type;
        
    }
    
    public function singSong(){
        for ($i = $this->noBeers; $i > 0; $i--){
            if ($i == 1) {
                $bottle = "bottle";
            }else{
                $bottle = "bottles";
            }
            $this->output .= $i . " ".$bottle." of beer on the wall \n";   
            $this->output .= $i . " ".$bottle." of beer \n";
            $this->output .= "And if 1 bottle of beer should fall,\n";
            if ($this->minusOne($i) == 1) {
                $bottle = "bottle";
            }else{
                $bottle = "bottles";
            }
            $this->output .= "There will be " . $this->minusOne($i) . " " . $bottle . " of beer on
the wall \n\n";
        }
        if (strtolower($this->type) == "html") {
            $this->addBreak();
        }
    }
    
    private function addBreak(){
        $this->output   = str_replace("\n","<br>",$this->output);
    }
    private function minusOne($int){
        return $int - 1;
    }
    public function getOutput(){
        return $this->output;
    }
}

$oBeer  = new SingBeerWallSong(99,"html");
$oBeer->singSong();
echo $oBeer->getOutput();

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Objects, phpdoc comments, exceptionsDave Marshall09/22/059
PHP5 code using classesTim M09/02/059

Comments

>>  Bau de jogos said on 11/24/06 20:47:20

Bau de jogos now this version is a lot better :)

it seems to be based on the Builder Design Pattern philosophy

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

Eric J K This one looks more structured than object-oriented...

Itīs using OO commands, but itīs still structured programming

>>  Philipp said on 05/28/07 17:22:49

Philipp This one is a very bad implementation.

1st it is not oo, but structured ... just thrown some functions into a useless class
2nd bad style, ineffective
3rd boring

ps when using php5 you might consider using the __toString method.

>>  Kenneth said on 02/26/08 13:11:39

Kenneth Thanks for the feed back and you are right, it is more of a library than a true class. On further research into PHP (yeaugh) the idea of OOP in PHP is completely flawed. Back to the C# and Java I go HOOAH!

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: