Jump to content

OOP Question -> Object to contain a collection of objects


enoch

Recommended Posts

Greetings,

 

The scenario:

class Book contains some simple information about a book such as title and width of book.

class BookShelf contains a collection of book objects which it can keep track of and also can monitor how many book objects it can fit based on their width.

 

Simple concept right? Well, I am having a bit of difficulty in creating the BookShelf class to contain and manage the book objects.

 

I believe the PHP SPL stuff may be the way to go on this but am not sure.

Any suggestions, links, or pointers would be greatly welcome!

 

Thanks,

Enoch

Link to comment
Share on other sites

Just adding some information and curious what you folks know about this:

 

The SplObjectStorage class (part of the SPL) seems ideal for this. 

http://us2.php.net/manual/en/class.splobjectstorage.php

 

Does anyone have experience using it?

If so, would I create class BookShelf (from the scenario) and then create a new SplObjectStorage object within it?  something along these lines:

 

class BookShelf{

public $shelf;

function __construct{

  $this->shelf =new SplObjectStorage();

}

}

 

and then work with it from there?  is that even the proper way to implement it... testing to commence - advice encouraged and appreciated!

 

}

 

Link to comment
Share on other sites

This is what I have so far... but it isn't working... yet.

 

<?php

// BOOK Class /////////////////////////

class Book{

  public $title = "";

  public $width = "0";

 

  function __construct($title, $width){

    $this->title = $title;

    $this->width = $width;

  }

 

}

///////////////////////////////////////

 

// BOOKSHELF Class /////////////////////////

class BookShelf{

public $shelf;

public $obj;

 

function __construct(){

  $this->shelf =new SplObjectStorage();

}

 

function addBook($obj){

  $this->obj = $obj;

  $this->shelf->attach($this->obj);

}

 

function removeBook($obj){

  $this->obj = $obj;

  $this->shelf->detach($this->obj);

}

 

function checkBook($obj){

  $this->obj = $obj;

  $this->shelf->contains($this->obj);

}

 

}

////////////////////////////////////////////

 

 

$b1 = new Book("Title1","2");

$b2 = new Book("Title2","2");

$b3 = new Book("Title3","2");

 

$bs = new BookShelf();

 

$bs->addBook($b1);

 

var_dump($bs->checkBook($b1));

echo "<br />";

 

$bs->removeBook($b1);

 

var_dump($bs->checkBook($b1));

echo "<br />";

 

?>

Link to comment
Share on other sites

Hi Daniel0 - thanks for taking a look at this stuff...

Couple things... the example I have here does not work- so it really is not ok.

 

1) ok, makes sense to have them private.

2) I am using the $obj as the variable to store the book object being worked on by the methods... am I missing something with this?

3) yep, no need for quotes on my numeric stuff ... g! :D

 

How familiar are you with the PHP SPL, in particular SplObjectStorage?  Why are these <tt>var_dump($bs->checkBook($b1));</tt>  returning nulls in the example code?

 

That would be okay, with a few exceptions:

 

1) Your properties shouldn't be public.

2) What is up with $this->obj = $obj; all over the place?

3) A width is a numeric value. Why are you storing it as a string? Use a float or int depending on your needs.

Link to comment
Share on other sites

2) I am using the $obj as the variable to store the book object being worked on by the methods... am I missing something with this?

 

Well, you can just do like this:

function addBook($obj){
  $this->shelf->attach($obj);
}

 

How familiar are you with the PHP SPL, in particular SplObjectStorage?  Why are these <tt>var_dump($bs->checkBook($b1));</tt>  returning nulls in the example code?

 

Because your method isn't returning anything.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.