0

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.

3 Answers 3

3

What version of PHP are you using.

Array dereferencing (referencing into a returned array) was only added in PHP 5.4.

If you're using a previous version, you'd have to do this:

$books = $BookBox->getThings();
echo $books[2];

Edit

Since you are pushing Books into a box, $books[2] returns you an instance of the Book object. Echo is used to output a string, hence the error.

You can either echo a particular property of the book, or print out all of the properties by doing:

print_r($books[2]);
Sign up to request clarification or add additional context in comments.

3 Comments

My version is 5.3.10. So you're saying if it was 5.4 my code would work?
@user1159454: Yes, exactly. However, I wouldn't recommend using any PHP 5.4 only features as webhosts won't roll it out for some time.
Alright, I'm still getting an error. Catchable fatal error: Object of class Book could not be converted to string
2

First, you cannot do echo since what you'll get is not a string but an object. Use print_r() or var_dump() instead.

Second, just like the other answers here, you should do this.

$books = $BookBox->getThings();
print_r($books[2]);

But I suggest you to make getThings() accept a variable for getting a specified array element:

function getThings($key = null) {
  if ($key) {
    return isset($this->storedArray[$key]) ? $this->storedArray[$key] : null;
  } else {
    return $this->storedArray;
  }
}

// Then you can do this
// print_r($BookBox->getThings(2));

Comments

0

What you are calling when you call $BookBox->getThings() is actually an object and not an array.

You might try something along the lines of:

$books = $BookBox->GetThings();
echo $books[2];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.