Need help passing object

I made up a silly example to learn OOP.

I can’t get the object that I passed into another object to work…


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>User Registration Form</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
		<link rel="stylesheet" type="text/css" href="styles/index.css">
	</head>
	<body>
		<h1>What kind of soup would you like today?</h1>
		<form method="post" action="results.php">			<!-- ACTION -->
			<!-- Menu Choices -->
			<div>
				<label>I'm not sure...</label>
				<input type="radio" name="soup" value="unsure" class="radio" /><br />
				<label>Broccoli:</label>
				<input type="radio" name="soup" value="Broccoli" class="radio" /><br />
				<label>Tomato:</label>
				<input type="radio" name="soup" value="Tomato" class="radio" checked="yes" /><br />
				<label>Chicken:</label>
				<input type="radio" name="soup" value="Chicken" class="radio" /><br />
				<label>Split Pea:</label>
				<input type="radio" name="soup" value="Split Pea" class="radio" /><br />
			</div>
			<!-- Submit button -->
			<div>
				<input type="submit" name="btnSubmit" value="Place Order" class="btn" id="btnSubmit" />
			</div>
		</form>
	</body>
</html>

<?php
	include ('classes/Soup.class.php');

	$objSoup = new Soup($_POST['soup']);

	echo $objSoup->getSoupType();

	$objBowl = new Bowl();

	$objBowl->fillBowl($objSoup);
	
	echo "The bowl contains " . $objBowl->getBowlContents() . "soup in it.";

?>


<?php
	class Soup{
		private $type;

		public function __construct($t=null){
			$this->type = $t;
		}

		public function getSoupType(){
			switch ($this->type){
				case "unsure":
					return "No soup for you!!";
					break;
				default:
					return "You chose " . $this->type . " soup.";
					break;
			}
		}
	}
	
	class Bowl{
		private $contents;

		public function fillBowl(Soup $s){
			$this->contents = $s;
		}

		public function getBowlContents(){
			if (is_null([B]$this->contents.type[/B])) {
				return "The bowl is empty!";
			} else {
				return "The bowl contains " . [B]$this->contents.type[/B] . " soup!";
			}

		}

	}

The areas in BOLD seem to be the issue…

TomTees


<?php
$bowl = new SoupBowl;
$bowl->addSoup(new Soup('Chicken'));

if($bowl->hasSoup()){    
    printf(
        'You have a nice bowl of &#37;s soup there!',
        $bowl->getSoup()->getType()
    );
}
?>


<?php
class SoupBowl
{
    protected
        $soup = null;
    
    public function addSoup(Soup $soup){
        $this->soup = $soup;
    }
    
    public function hasSoup(){
        return $this->soup instanceof Soup;
    }
    
    public function getSoup(){
        if($this->hasSoup()){
            return $this->soup;
        }
    }
}

class Soup
{
    protected
        $type;
    
    public function __construct($type){
        $this->type = $type;    
    }
    
    public function getType(){
        return $this->type;
    }
}

If it’s private, you need a method to retrieve it.

$this->contents->get_type();

And in your Soup Class, you need a

function get_type() {
return $this->type;
}

Remember a private variable is only accessible within the object itself - therefore you need a method to retrieve it for you.

Thanks but that didn’t work because I have a private variable.

I’m trying to return the type of Soup that is in the Bowl.

Can someone look at or run my code and see where I’m going wrong?

TomTees

$this->contents->type

Remember in PHP, a dot is a concatenator (sp?)