enoch Posted September 4, 2009 Share Posted September 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
enoch Posted September 4, 2009 Author Share Posted September 4, 2009 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! } Quote Link to comment Share on other sites More sharing options...
enoch Posted September 4, 2009 Author Share Posted September 4, 2009 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 />"; ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 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. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 4, 2009 Share Posted September 4, 2009 not quite sure why you need the bookshelf class - perhaps the book object itself would benefit from having a shelfmark member and status (whether its in lib or not). Just appears to me that would provide with a simpler solution... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 not quite sure why you need the bookshelf class Not if you're like me and your books are all over the place at least. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 4, 2009 Share Posted September 4, 2009 Quote Link to comment Share on other sites More sharing options...
enoch Posted September 5, 2009 Author Share Posted September 5, 2009 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! 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 6, 2009 Share Posted September 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.