0

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>
  • 1
    What does this $_GET an the foodArray have to do with the class functions? Please explain it clearly. – Indrasis Datta May 01 '16 at 04:22
  • Give more information, be clear please. – olibiaz May 01 '16 at 06:01
  • Sorry the `$_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`. Does that make more sense? I'm sorry I'm not explaining it properly! – cigarette_unicorn May 01 '16 at 11:19

2 Answers2

1

I'm not exactly sure what your final intention is but I can try to point out a couple of places where you are going wrong:

  • The setFoodValue() method of the FoodArray object is never called so $foodValue has no value
  • $foodArray is set as an array but is immediately overwritten when you call the line $foodObject->getFoodValue()
  • $foodObject->getFoodValue() actually returns nothing because $foodValue was never set

There should be no difference in your getters and setters if you are passing an array or a string you could pass them and retrieve them the same way.

Again not sure exactly what you are trying to accomplish but you could try something like this:

$foodObject = new FoodArray;

$foodArray = array ("Salad", "Vegetables", "Pasta", "Pizza", "Burgers");
$foodObject->setFoodValue($foodArray);

$foodObjectArray = $foodObject->getFoodValue();

echo "The number you chose is ". $foodNumber;
echo "The food item you choose is".$foodObjectArray[$foodNumber]; 
Chaim
  • 2,109
  • 4
  • 27
  • 48
  • You are awesome!! This is exactly what I was trying to explain. You must be some kind of cryptic code breaker having deciphered it from my poor explanation! Hahha! Thank you so so much!!!! – cigarette_unicorn May 01 '16 at 12:25
0

If the problem is that echo "The food item you choose is".$foodArray[$foodnumber]; doesn't produce what you expect, there are two potential problems:

  1. I'm not clear on the intent of your $foodObject. However, the $foodArray=$foodObject->getFoodValue(); may be messing it up, unless the internal $foodValue property is (like) the $foodArray you declare with Salad, Vegetables, etc.
  2. When echo-ing $foodArray[$foodnumber], the 'n' in number is lowercase where above you set $foodNumber (capital 'N')
andrianoid
  • 51
  • 7
  • Thank you for your reply! 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. I haven't my head fully around the terminology of PHP yet so I apologise if I've made it more confusing! – cigarette_unicorn May 01 '16 at 11:31