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

(PHP5 code using classes)

Date:09/02/05
Author:Tim M
URL:n/a
Comments:9
Info:n/a
Score: (2.61 in 38 votes)
<?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

VersionAuthorDateCommentsRate
Objects, phpdoc comments, exceptionsDave Marshall09/22/059
another versionKenneth Clark05/17/054

Comments

>>  Ed said on 09/12/05 14:57:26

Ed It doesn't run, holmes.

>>  Tim M said on 09/22/05 20:04:26

Tim M It did work for me. What errors do you get then? And are you sure you are using PHP5 instead of PHP4?

>>  Tom H said on 12/10/05 20:53:48

Tom H It ran very fast for me on a PHP5 server.

The '\r\n' did not come through, so no new lines, just a block of script.

(could be a setting on my machine)

TH

>>  James C said on 01/08/06 00:47:51

James C Worked very well running with the php.exe binary on windows ;)

>>  toxik said on 02/12/06 02:48:10

toxik Umm, function Stream($doStream = true) seems a little bit weird, why call it Stream if you can pass it an argument to tell it that it shouldn't stream?..

>>  Paul said on 10/22/06 14:52:09

Paul It doesn't work dude.

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

>>  Silent Hunter said on 10/25/06 00:17:34

Silent Hunter Man, are you using PHP5? This error message only shows on PHP4 because of an unknown keyword (private), since this is OOP basic that was a lack in PHP4 that's why PHP5 was developed...

>>  Paul said on 11/01/06 02:56:54

Paul yeah, it was PHP4, sorry I'm a noob in PHP. It works fine with PHP5
Sorry for the noise.

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

Bau de jogos this code only serves to show the programmer sucks a lot in OO programming :(

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: