wrathican Posted April 12, 2008 Share Posted April 12, 2008 hey there, i recently thought i better start looking into OOP to see what all the fuss is about. i like kinda like the whole concept of it. ive looked over a few tutorials on it. one that gave me a better insight was how to create a OOP CMS. this however did not give me any real idea of how to use OOP. it just went over the syntax and layout and such. A friend recently asked me to create a website for his photography portfolio. so, i thought why not try my hand at OOP? the only thing is, how many classes should i have? i have a class that defines all my 'settings' (so DB stuff), and a class for basic methods, which extends the settings class, l ike getting an array, executing a query and connecting to the DB. being a photographer he obviously is going to have alot of images to display. so im going to create a an image gallery. ive been thinking of methods i would need to create the image gallery and there not many. they all just seem to extend the previous class. is that a good thing? e.g class a { //some things } class b extends a{ //some things } class c extends b{ //some things } also, should i have a class for everytime i want to do something different? like run database stuff, define settings, display the gallery items? Link to comment https://forums.phpfreaks.com/topic/100832-oop-classes-how-many/ Share on other sites More sharing options...
448191 Posted April 12, 2008 Share Posted April 12, 2008 I didn't read the whole post, but here's some quick advice: avoid more than two levels of inheritance. Favour (aggregate) composition over inheritance. Google that last phrase, you'll get tons of info. Link to comment https://forums.phpfreaks.com/topic/100832-oop-classes-how-many/#findComment-515694 Share on other sites More sharing options...
keeB Posted April 12, 2008 Share Posted April 12, 2008 I made a gallery before. From what I remember my core objects were, Gallery, which contained a list of Albums, which contained a list of Images All Images could draw themselves, resize themselves, etc. Link to comment https://forums.phpfreaks.com/topic/100832-oop-classes-how-many/#findComment-515732 Share on other sites More sharing options...
deadonarrival Posted April 17, 2008 Share Posted April 17, 2008 Try singleton classes for things like your database connections. Other then that, just think of the objects of the program. A gallery is an object, as is an album and an image image, so is a user. Link to comment https://forums.phpfreaks.com/topic/100832-oop-classes-how-many/#findComment-519263 Share on other sites More sharing options...
Cobby Posted April 19, 2008 Share Posted April 19, 2008 There isn't really any limitation on how many class you can have defined. Are the images stored in the database? Generally, you would have a class to handle the DB connection and provide a few functions to save a bit of SQL. Then you would have a gallery class which you would pass the instance of the DB class to the constructor and assign it to a property. Then you would have functions in your gallery class that do that actually work, like getAlbums(), getPhoto($photo_id), etc. Link to comment https://forums.phpfreaks.com/topic/100832-oop-classes-how-many/#findComment-521047 Share on other sites More sharing options...
cowfish Posted May 6, 2008 Share Posted May 6, 2008 Don't use more than you need, but don't use less either. Remember that a class usually has class variables (also known as instance variables) and methods (also known as functions). If you discover that your class only have methods but very little or no class variables, then it's likely that class should not exist because it's not a good class choice. Chances are you are trying to program procedurally using an OO language. On the other hand, if you find your class having tons of class variables and hardly any methods, ask yourself again whether the class should exist. A class is not a data store. A class should represent an IDEA. It does not present a process and definitely not a data store. The general rule of thumb is that classes should be NOUNS. There are exceptions to the rule of course. Finally, a class should only be responsible for itself and itself only. For example, the responsibility of a Car class is to move, turn windscreen wipers on/off, lock doors etc. Do not make the Car do something completely unrelated to what a Car does. Do not try to make a class do many things. I often see god classes that try to cover so many things with so many unrelated methods. That's not good practice and defeats the point of OOP. Link to comment https://forums.phpfreaks.com/topic/100832-oop-classes-how-many/#findComment-534136 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.