Member
Join Date: Jul 2004
|
i have never understood the benefit of classes in PHP. actually, i don't even understand what they do or why i need them. i do want to learn to use them though. i'm sure they're there for a good reason.
the PHP manual does pretty much nothing for me in this regard, although it's excellent otherwise. will someone please explain why i need classes in PHP? |
quote |
Microbial member
|
Well, object orientation in PHP isn't the best implementation out there, but basically classes are a really good way of grouping together related data and functions so they're easier to use. Used properly, your code should become more readable and modular, and therefore more maintainable.
The benefits of OO are a big topic… I've leave it to Kickaha or similar to explain it fully. |
quote |
Selfish Heathen
Join Date: May 2004
Location: Zone of Pain
|
A "class" isn't specific at all to PHP. As staph alluded, it's a construct of OO programming in many languages.
Here's my very basic explanation. A class is a modular piece of code, a self contained set of variables and methods (or functions or procedures, I never really knew the difference between these three terms). You typically make a class based on some objects and methods that work well together. When you create a new instance of a class, you create a new instance of each of its variables. You can also make new classes that are based on other classes, adding additional functionality and redefining how certain parts of the existing parent class work. An example. Say you write a program for a car dealership. You want to keep track of all the different types of cars and their features. You could create a basic "car" class that has variables like isAvailable, price, mileage, mpgCity, etc. with a method goTestDrive that makes it temporarily unavailable and adds mileage to it. Each car on the lot could be an instance of the car class. You could extend that car class to, say, a sedan class and a truck class that have the same variables and functions already defined for the car class, but they have additional new ones like the truck's extendWench method or the sedan's rearSpoiler variable. Does that help? This example only begins to scratch the surface of the benefits and features of classes. Look up "polymorphism" and "inheritance" when you have some time to kill. The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting. |
quote |
Veteran Member
|
Quote:
|
|
quote |
Member
Join Date: Jul 2004
|
man, i feel so dense... but i still don't get that feeling of "whoooaaa, i gotta use this!!".
like the car dealership, why would i not just use data from the database to specify price, milage, availability etc? i want to understand... |
quote |
Selfish Heathen
Join Date: May 2004
Location: Zone of Pain
|
Who says you're allowed to use a database? What if you don't want to constantly make various queries on the database? How would you deal with that type of scenario?
Remember: this isn't just about logically grouping variables together; it's also about grouping methods with them. Sure, you could make everything a global variable and allow anything anywhere to change any variable, but that's risky because it allows for mistakes by letting you use an unrelated method on a particular variable. Separating code into classes allows you to protect your data from yourself (or from other programmers). For example, many programmers use private variables in classes and implement getter and setter methods to abstract the variables away from the programmer, protecting them from being misused. The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting. |
quote |
Member
Join Date: Jul 2004
|
thanks brad! and keep it coming everyone!
|
quote |
is the next Chiquita
Join Date: Feb 2005
|
and just so I know-
how would a class be different from oh say, a public sub which could do basically the same thing and run only when you call it? |
quote |
‽
|
Quote:
Whereas a class has methods/functions and attributes/variables that can either apply to the class (all instances) or one specific instance. E.g., for a class "Address", you can have a class method "deleteAll" that will get rid of all addresses, or an instance method that deletes only one particular one. For a subclass "Friends' Addresses", you can have a class variable "isFriend" that's true, and once you decide you don't care to have friends any more, you can set that to false (or, you could just trash the class), whereas you can have an instance variable "name" that's obviously different for each and every instance. Needless to say, my examples suck — but that's another matter. |
|
quote |
is the next Chiquita
Join Date: Feb 2005
|
Cool- good to know.
Thanks again. |
quote |
Member
|
The others have been doing a good job, but I'll throw my 2 cents in.
Brad said: Quote:
Code:
class Car
{
// variables
float speed;
fload direction;
// methods
accelerate()
{
speed += 10;
}
decelerate()
{
if (speed - 10 < 0)
{
speed = 0;
}
else
{
speed -= 10;
}
}
turnLeft()
{
direction = (direction -90) % 360;
}
turnRight()
{
direction = (direction +90) % 360;
}
} Ok, that should be a very simple view of a car. Now to do something with it, we need to make an actual object from our blueprint. This is called instantiating an object. Each object we make from a class is known as an instance of that class. Something like this:Code:
Car myCar = new Car();
Car yourCar = new Car(); Now we have two Car objects called myCar and your Car. We can use the methods we defined to interact with these cars.Code:
myCar.turnLeft();
myCar.accelerate();
youCar.accelerate();
yourCar.turnRight();
yourCar.decelerate(); Hopefully this is helpful to you. If you don't understand something let me know."Slow vehicle speeds with frequent stops would signal traffic congestion, for instance." uh... it could also signal that my Mom is at the wheel... Last edited by Mr Beardsley : 2006-07-13 at 15:09. |
|
quote |
Senior Member
Join Date: Jan 2005
Location: London, UK
|
Also try this for size:
http://developer.apple.com/documenta...3-CH8-CJBIJAFF I found it helped my understanding much better... |
quote |
Posting Rules | Navigation |
|
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
PHP vs all the others? | nassau | Programmer's Nook | 18 | 2006-06-29 03:49 |
PHP: is this a magic function? | drewprops | Programmer's Nook | 10 | 2006-03-22 09:36 |
PHP installation issues | ThunderPoit | Programmer's Nook | 17 | 2006-03-17 11:25 |
PHP: making index files work | drewprops | Programmer's Nook | 7 | 2005-11-21 12:15 |
Generating XML for Newsfeed (MovableType and PHP) | drewprops | Genius Bar | 3 | 2005-01-22 00:32 |