I'm experimenting with classes and objects for the first time and I thought I'd make a template for a Box that can store things like Books. (Thinking in terms of real-world items)
<?php
function feetToInches($feet){
$feet = $feet * 12;
return $feet;
}
class Book{
var $l = 6;
var $w = 5;
var $h = 1;
}
class Box{
//This is a box. It has length, width, and height, and you can put things in it.
var $length = 0;
var $width = 0;
var $height = 0;
var $storedArray = array();
function setDimensions($l, $w, $h){
$this->length = feetToInches($l);
$this->width = feetToInches($w);
$this->height = feetToInches($h);
}
function storeThings($thing){
$this->storedArray[] = $thing;
}
function getThings(){
return $this->storedArray;
}
}
$thatBook = new Book;
$BookBox = new Box;
$BookBox->setDimensions(6,5,1);
for($i = 0; $i < 5; $i++){
$BookBox->storeThings($thatBook);
}
echo $BookBox->getThings() . "<br />";
/*
foreach($BookBox->getThings() as $item){
echo $item;
}
*/
var_dump($BookBox);
?>
So what I have is simple here, you have boxes of a dimension, and you throw books of a fixed dimension in them.
Putting things in it is no problem, but when I try to retrieve them, I either get errors or nothing happens. And when I try to specify a key for the array like
echo $BookBox->getThings()[2];
I get an error that it's not an array or something.
So can someone please point me in the right direction here?
And normally the class would be a separate file, but I'm just learning here.