Re: [PHP] OOP, Classes, Sharing Code
Hello! Another option would be to have a generic List class that displays items in a list. Both your Album and Photo class should implement a method like getNextItem or the PHP5-native iterator methods so the List class can iterate over a class assigned to it without knowing what it is. If the properties of Album and Photo are too different to generate a list, you could make a generic list class and subclass it for Album and Photo. If you want to know more about OOP, I recommend this book: http://www.amazon.com/exec/obidos/tg/detail/-/0201715945/ qid=1088493890/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/102-6243709-1202536? v=glances=booksn=507846 It helped me a great deal understanding how to think object oriented. A third option would be to use the Smarty templating system. With templates you would only have to write a small portion of HTML code for each list. With best regards Gabriel Birke -- KONTOR4_Neue Medien Plathnerstraße 5 30175 Hannover Fax: +49 51184 48 98 99 mailto:[EMAIL PROTECTED] http://www.kontor4.de Am 28.06.2004 um 20:32 schrieb Joel Kitching: I'm kind of new to OOP, so bear with me here... I have a portfolio which contains albums. These albums contain photos. So it would be natural to declare the classes: Portfolio Album Photo It seems to me that none of these classes have the is a relationship, and therefore can not be extended from each other. The reason I wish to do this, is because listing albums and listing photos use almost exactly the same code. i.e. // The constructor would automatically fill in the album's variables from the database. $album = new Album($album_id); // This would print out the HTML for the list of photos. $album-list_photos(); ... or the same thing with Portfolio, which would list the albums. The only thing that's different is the links to which the anchors are pointing, and the content. (Thumbnails.) So my question is, should I just duplicate the code in each class (Portfolio and Album), or is there a better way of organizing all of this? -- Joel Kitching http://midgardmanga.keenspace.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP, Classes, Sharing Code
Joel Kitching [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] What generic class name would be appropriate in this case? I just can't see how I would link the two together. Also, they would have to use fairly generic variables names if I were to do this. Like using the variable $items for the array of classes of either albums or photos. I am using AbstractItemList, and then subclass PhotoList, and others form it. Abstract item list takes care of repeating items, pagination, and displaying navigation between listing pages. Concrete classes know how to display item in list (itemAsListElement()), know how many items there are in total, and getNextItem iterator. There is a bit more but this should be enough to get you started. rush -- http://www.templatetamer.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] OOP, Classes, Sharing Code
I'm kind of new to OOP, so bear with me here... I have a portfolio which contains albums. These albums contain photos. So it would be natural to declare the classes: Portfolio Album Photo It seems to me that none of these classes have the is a relationship, and therefore can not be extended from each other. The reason I wish to do this, is because listing albums and listing photos use almost exactly the same code. i.e. // The constructor would automatically fill in the album's variables from the database. $album = new Album($album_id); // This would print out the HTML for the list of photos. $album-list_photos(); ... or the same thing with Portfolio, which would list the albums. The only thing that's different is the links to which the anchors are pointing, and the content. (Thumbnails.) So my question is, should I just duplicate the code in each class (Portfolio and Album), or is there a better way of organizing all of this? -- Joel Kitching http://midgardmanga.keenspace.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] OOP, Classes, Sharing Code
[snip] So my question is, should I just duplicate the code in each class (Portfolio and Album), or is there a better way of organizing all of this? [/snip] If you are going to duplicate code why not just create a generic class with the code that would be duplicated and then extend the class as required? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP, Classes, Sharing Code
On Mon, 28 Jun 2004 13:41:19 -0500, Jay Blanchard [EMAIL PROTECTED] wrote: If you are going to duplicate code why not just create a generic class with the code that would be duplicated and then extend the class as required? What generic class name would be appropriate in this case? I just can't see how I would link the two together. Also, they would have to use fairly generic variables names if I were to do this. Like using the variable $items for the array of classes of either albums or photos. Or maybe this is exactly what OOP is about and I'm just starting to get it. -- Joel Kitching http://midgardmanga.keenspace.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP, Classes, Sharing Code
Joel Kitching [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Mon, 28 Jun 2004 13:41:19 -0500, Jay Blanchard [EMAIL PROTECTED] wrote: If you are going to duplicate code why not just create a generic class with the code that would be duplicated and then extend the class as required? What generic class name would be appropriate in this case? I just can't see how I would link the two together. Also, they would have to use fairly generic variables names if I were to do this. Like using the variable $items for the array of classes of either albums or photos. Or maybe this is exactly what OOP is about and I'm just starting to get it. Hi Joel, from what you wrote it seems that only the type of display is similar in both classes. I don't think this is enough to extend them from the same base class because the class properties and update/insert/load methods will be different in each. You have more of a 'is part of' relationship here. So I'd suggest something like this (simplified): class Portfolio { var $portfolioID; var $albums = array(); } class Album { var $albumID; var $portfolioID; var $photos = array(); } class Photo { var $photoID; var $albumID; var $name; } Just my 2c. Regards, Torsten Roehr -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP, Classes, Sharing Code
Look correct from my point of view... class Portfolio { var $portfolioID ; var $albums = array () ; function Portfolio ( $newID ) { $this-portfolioID = $newID ; } function addAlbum ( $album ) { $this-albums[] = $album ; } function getAlbum () { return current ( $this-album ) ; } } some knowlege on the iterator pattern is required though But you could even use a stack ( which i prefer ) class main { var $stack = NULL ; function create ( $ID ) { $this-stack = new stack ( $ID , $this-stack ); } function remove ( $ID ) { $this-stack = $this-stack-remove(); } function getID () { return $this-stack-getID(); } } class stack { var $ID ; var $prev = NULL ; function stack ( $ID , $prev ) { $this-ID = $ID ; $this-prev = $prev ; } function getID () { return $this-ID ; } function remove () { return $this-prev ; } } $foo = new main(); $foo-create( 100 ); $foo-create( 200 ); echo $foo-getID();// 200 $foo-remove(); $foo-create( 300 ); echo $foo-getID();// 300 $foo-remove(); echo $foo-getID();// 100 $foo-remove(); unset ( $foo ); [...] from what you wrote it seems that only the type of display is similar in both classes. I don't think this is enough to extend them from the same base class because the class properties and update/insert/load methods will be different in each. You have more of a 'is part of' relationship here. So I'd suggest something like this (simplified): class Portfolio { var $portfolioID; var $albums = array(); } class Album { var $albumID; var $portfolioID; var $photos = array(); } class Photo { var $photoID; var $albumID; var $name; } [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php