If classes were simply groups of functions, they wouldn't be very useful, as you say. But they are far more than that. The fact that classes can contain properties (variables specific to the class) is what makes all of the difference. Because of this you can completely encapsulate certain functionality into objects.
Nearly all people new to OO don't understand its significance because they look at classes as merely groups of functions. It is difficult for anyone to see the benefits without writing some code for themselves. The first few classes you write may not seem to do you much good. But soon the methodology will hit you and it will make a world of difference. You will find that though functions are reusable, the kind of reusability you eventually reach with classes will far exceed what you thought possible.
Quote:
Originally posted by Stefan
as for the idea of recoding without other things changing: functions can also be recoded without the need of rewriting the rest of the system. they just get called, and return something.
|
Sure you can re-write the way the function works on the inside but if you wanted to change the way you were working with whatever data or how you worked with it, you would mess up the every script that uses it.
Say you had a script that had several variables and several functions that performed operations on those variables. The functions would take arguments and return new values, and the code in your script would decide how to pass those variables to the functions and how to interpret the output. But let's say you had 10 scripts that all did very similar things but had minor differences in the variables and the way they were manipulated. Sure, you could reuse the functions but without the variables being encapsulated within them, you would be re-writing the same code over and over to deal with what those functions were doing to the variables.
What would happen if you decided, after writing 10 scripts to work with a certain piece of data, that another operation had to be performed on the data around the middle of the code? You may have to add code into 10 scripts and risk breaking all 10 of them. Or, in with an object you could simply add the respective code to the class and every script that uses that class would take advantage of the change transparently. Remember that the class can keep up wtih every property related to that object that you want it to. Sure, if the last function you call in each script is display() and you decided you needed to filter the data one last time you could add the code to the display() function and not need to add code to all 10 scripts. But what if the filter depended on a condition, say on the value of a variable that you aren't currently passing to that function? Or what about several variables? At this point you would have to add several arguments to the function and rewrite every single call to the function so that the extra variables were passed to it. What if you called the function 100 times throughout your code? In OO, because of the encapsulation of all properties and methods into one object, every variable related to that object would already be available to the object by referring to $this, so you would truly only have to change the code in one place - the class itself.
Remember that a key factor in making OO work is the fact that classes can perform operations on themselves using the keyword $this to refer to the current object.
Furthermore, if you later decided a single script needed to work with several instances of the same data, how would you accomodate all of the variables in an organized way? By changing everything to arrays? Maybe not the best idea. That may end up being a lot of variables that would have to be changed to arrays. With OO you could simply have an array of objects with all respective variables encapsulated inside.