|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 ful? cheers Col. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 ful? > > 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 ================== |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Yes, the benefit of using a class to encapsulte the "who is"
functionality, is that anyone can now grab that one class and import it into their programs. Its guaranteed not to interfere with anything they have already created. Jerry Stuckle wrote: > 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 ful? > > > > 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 > ================== |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Kay;115580 Wrote: > Yes, the benefit of using a class to encapsulte the "who is" > functionality, is that anyone can now grab that one class and import > it into their programs. > > Its guaranteed not to interfere with anything they have already > created. Unless the name of the class is already used .FFMG -- 'webmaster forum' (http://www.httppoint.com) | 'Free Blogs' (http://www.journalhome.com/) | 'webmaster Directory' (http://www.webhostshunter.com/) 'Recreation Vehicle insurance' (http://www.insurance-owl.com/other/car_rec.php) | 'Free URL redirection service' (http://urlkick.com/) ------------------------------------------------------------------------ FFMG's Profile: http://www.httppoint.com/member.php?userid=580 View this thread: http://www.httppoint.com/showthread.php?t=24466 Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing). |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Tue, 22 Jan 2008 06:15:36 +0100, FFMG <FFMG.33kobz@no-mx.httppoint.com>
wrote: > > Kay;115580 Wrote: >> Yes, the benefit of using a class to encapsulte the "who is" >> functionality, is that anyone can now grab that one class and import >> it into their programs. >> >> Its guaranteed not to interfere with anything they have already >> created. > > Unless the name of the class is already used .*sigh* PHP6 will have namespaces I hear ![]() -- Rik Wasmus |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Jan 21, 6:07pm, co...@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 ful? > > cheers > > Col. Jerry - what you've described could also be a function as well, not that I'm trying to belittle you. This is what I wrote when asked the same question, although it was actually describing JavaScript classes :- "A class is just a way of describing what something looks like and how it works, like a template. For instance, suppose we had a class called 'Car'. We can create an 'instance' of the class called 'VW Beetle'. Our new copy of 'Car' has all the things the 'Car' class has, such as wheels, mirrors, doors, etc - these are called 'Properties' It also has actions, such as drive, park, reverse, etc - these are called 'Methods'. We can use the 'Car' template to create as many copies as we want (using the 'new' keyword). Each copy is known as an 'Object', and although it has all the features of the class, it is a unique instance." In addition to this, one of the most powerful features of a class is it's inheritance, which you can't do with a function. Suppose we have a class called 'Vehicle'. We could say it has certain properties, such as wheels, colour, size, etc. With this base class, we can also create a whole new class, called 'Car' that inherits all of the features of the 'Vehicle' class. We can also add specific 'Car' methods and properties to our new class, such as engine, ignition, gears, etc. We can also overwrite the methods of a base class, so instead of the 'ChangeGear()' method of our Vehicle class effecting just the gears, we could get it to change the gear and display the current gear on the dashboard. Obviously Vehicles and Cars is not a real world example, but it gives you some point of reference about how the models work together, and is only one good use of classes. I do agree with you that this can all turn into spaghetti code, but if used correctly, becomes immensely powerful. Rob. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Rob wrote:
> > Jerry - what you've described could also be a function as well, not > that I'm trying to belittle you. > <snip> > > Rob. > > I disagree. Functions have behavior. Objects have both state and behavior. What I described was an object. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
In article <S7-dnSyA-K1uaAnanZ2dnUVZ_uGknZ2d@comcast.com>,
jstucklex@attglobal.net says... > > But this is all very simplistic. I really recommend you visit the > library and check out the books on object oriented programming. > > Like I said - I understand classes and object oriented programming - it isnt an issue. This is what I asked: 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. To me this appears to be contra-intuative. I'm looking for reasons why people are doing this as a matter of course with PHP cheers Colin |
|
![]() |
| Outils de la discussion | |
|
|