PDA

View Full Version : please explain 'class' in PHP


nassau
2006-07-12, 17:43
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?

:)

staph
2006-07-12, 17:58
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.

Brad
2006-07-12, 22:00
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. :p

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.

scratt
2006-07-12, 22:29
Look up "polymorphism" and "inheritance" when you have some time to kill.

Argggghhhhhhhhhh! ;)

nassau
2006-07-13, 02:04
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...

Brad
2006-07-13, 07:48
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.

nassau
2006-07-13, 07:56
thanks brad! and keep it coming everyone!

Banana
2006-07-13, 09:45
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?

chucker
2006-07-13, 09:55
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?

A sub doesn't hold data. It can have functions and variables, but no distinction between class and instance. That is, every sub will have the same variable values. Changes apply to all "instances" of subs, because there are no instances.

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. ;)

Banana
2006-07-13, 10:50
Cool- good to know.

Thanks again.

Mr Beardsley
2006-07-13, 11:33
The others have been doing a good job, but I'll throw my 2 cents in.

Brad said:
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.

That is totally correct, and this grouping of data and behavior inside a class is "Encapsulation". Think of a class as blueprint for an Object. An Object can be anything. Think of things around you: desk, chair, person, building, etc. Let's us Brad's car for an example. We'll write in sudo code a simplified blueprint for a car.


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:


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.


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.

mooty
2006-07-13, 13:33
Also try this for size:

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_3_section_1.html#//apple_ref/doc/uid/TP30001163-CH8-CJBIJAFF

I found it helped my understanding much better...

chucker
2006-07-13, 13:36
And for what it's worth, there's another thread about OOP here (http://forums.applenova.com/showthread.php?t=13312). Ignore the fact that I had started it and all.