I'm extremely new to php so excuse my code if you think it is a mess but I've been trying to figure this out for hours.
I am trying to use the getter and setter method to pull functionality from a class then use an array to create new objects based off the class. $_GET
is to get the input foodNumber
from a HTML form which determines which position in the array is chosen. So if 0 is inputted on the form, it represents Salad
, if 2 is entered on the form it should mean Vegetables
.
So in my mind, the $foodObject
is creating a new object from the class FoodArray
. I am trying to make the objects be in an array, so in this case $foodArray
. But I don't know how to input values through an array using the getter setter method using the class functions or how to call them.
Thanks in advance!
<?php
class FoodArray {
private $foodValue;
public function setFoodValue($x){
$this->foodValue=$x;
}
public function getFoodValue(){
return $this->foodValue;
}
}
$foodNumber = $_GET['foodNumber'];
$foodObject = new FoodArray;
$foodArray = array ("Salad", "Vegetables", "Pasta", "Pizza", "Burgers");
$foodArray=$foodObject->getFoodValue();
echo "The number you chose is ". $foodNumber;
echo "The food item you choose is".$foodArray[$foodNumber];
?>
/////HTML FORM/////
<html>
<body>
<form action="class_with_getter_and_setter.php" method="get">
<b>Choose a number between 0-4 and get a mystery food!</b>
<input type="text" name="foodNumber">
<input type="submit">
</form>
</body>
</html>