tags:

views:

123

answers:

6

I start to learn class in PHP. According to my experience with other language, i can tell class is different then function. So, how we spot if our class start to be too much and too important and how we minimize its impact ?


EDIT : The question is more able how to spot pattern of the writing class problem. Personally. I expect an answer based on experience.

+2  A: 

A function should be used when you have some code that can be called several times from several distinct locations on your application, to avoid copy-pasting the code.

A class should be used to represent an entity :

  • the data of something
  • and the methods (behaviors) that can be applied to those data

There's no real notion of too much : what matters is that the class contains what makes the entity ; nothing more, nothing less.

Pascal MARTIN
This is not necessarily true. You can represent functions as first class objects, for example if you are applying the command pattern. Most algorithms can be encapsulated as classes which can be called several times from several distinct locations.
Duncan
+1  A: 

I don't really understand what you mean by "be too much and too important and how we minimize its impact". Object-oriented programming is a paradigm that arose because it's a more natural way to think about and organize solutions to problem sets than just functions-operating-on-data. Because it's a more natural way to think about most problems, the real question should be "how can we tell when we're not using object orientation effectively and refactor our code to improve our solution?"

Dathan
yes, that's it. thank you. i'm start to learn the jargon. pascal Martin almost answered. He describe it the fundamental different why we rely on function or class. And he help me as somebody who rely to function.
justjoe
+1  A: 

Generally Function is used to reduce the data redundancy. While your class is wrapping all of your function into one, and also secure your data and other functions which can only be used by their own class objects.

ravindrakhokharia
+1  A: 

I reckon by your post that you're probably using Classes as a repository of functions with no real realtion between them. (I might be wrong, so sorry if I'm just jumping to conclusions)

Classes are extremely usefull and are the base of OOP.

Using an extremely abusive analogy:

Imagine something like a PaperSheet.

A PaperSheet has properties like Size, Color, Shape, Material.

You can do stuff with a papersheet like write something in it, fold it in a origami (=P), etc..

So using programing language:

PapaerSheet is a Class

Size, Color, Shape, Material are variables, proprieties of the Class PaperSheet

Write_Something_In_It, Origami_It are functions of this class

To perform Write_Something_In_It you just need to provide some text, but to perform Origami_It you will need to provide more instructions, since making a Swan is different of making a Toad.

SO, coding it would be something like this:

<?
   class PaperSheet {
        var $size;
        var $color;
        var $material;
        var $shape;

        function Write_Something_In_It($text) {
            /// Some Code
        }
        function Origami_It($OrigamiShape) {
            if ( $OrigamiShape < $this->$shape ) {
                          echo "You can do the origami";
                  } else {
                          echo "You can't do the origami";
            }

        }
    }
?>

So PaperSheet Class defines what a Paper Sheet is and what you can do with it. And since there are lots of idenctical Paper Sheet in your office, every time you grab a Paper Sheet from the stack you "create" a new Object of the Class PaperSheet.

You can even create 100 PaperSheet Objects (named differently, of course) and store them in a cabinet (array).

Now, the variables you see above should only make sense in the context of a PaperSheet, and should only be used in the context of the PaperSheet class.

Smae goes for Functions.

If you have other classes like Table, for instance, that also has a size and color, it doesn't make sense if it has a size of an A4 sheet.

And you shouldn't be able to Origami a Table (although if you could, that would be awsome).

So, in an OOPL, Classes are extremely usefull as they not ony wrap relating code, as they give a more intuitive way of using it.

Hope it helps

Tivie
it's really helpful.
justjoe
+1  A: 

A class is 'too much' when you start searching for a piece of code in that class. You know it must be there, but you keep scrolling in the class code. This is a strong sign that this class does 'too much'.

The same btw. goes for functions. If you're searching for your functions, you have 'too much' of them in one place.

Sebastian P.R. Gingter
thanks this help me spot the problem pattern
justjoe
is there any practical sign based on your experience ?
justjoe
I read the book Clean Code from Robert C. Martin. He shows up what 'code smells' are for him. Classes doing 'too much' is one of them. I suggest you read his book. There are a lot of good (imho) signs explained there - with suggestions on how to remove them and make your code more structured (and thus more maintainable).
Sebastian P.R. Gingter
+2  A: 

Describe what your class does. If you use the word 'and' in the description, it's doing too much. A class should do one thing. The same goes for functions.

Duncan
i like the 'and' in the description. ;D it's brilyan
justjoe