|
Posted by Jerry Stuckle on 01/21/08 19:42
colin@nospmanthanks.com wrote:
> I understand what a class is but am confused as to why so many people are
> using them for what appear to be very simple PHP applications. I guess I
> dont know enough about how they are useful.
>
> for example I've just seen a "who is" application thats just 2 files
> and a few dozen lines of very simple code
>
> But the author put the thing into a class. This just seems to complicate
> the code without providing any benefits as far as I can see.
>
> Can someone explain what the advantage of this is please? And any advice
> on when or when not to use classes would be helpful?
>
> cheers
>
> Col.
>
Entire books have been written on Object Oriented Programming. But the
main advantages are to encapsulate the object so you don't have to worry
about details outside of the object (an object is in instantiation of a
class).
For instance - floating point numbers are kept internally as base +
mantissa. To add two floating point numbers, the system has to adjust
one so that the bases are the same, then add the mantissas. Depending
on the result, it may adjust the bases again.
But all of this is transparent to you, the user. The actual operation
is encapsulated.
Properly designed, the code can be reusable. For instance, if I have a
class representing a table in a database. I can put all of the SQL code
in the class, and the rest of the program doesn't have to worry about
the database. I can even change databases or change to a flat file or
XML and all I have to change is the code in the class. Nothing else
needs to be changed.
But this is all very simplistic. I really recommend you visit the
library and check out the books on object oriented programming.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|