Jump to content

OOP Classes - How many?


wrathican

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

  • 3 weeks later...

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
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.