User Name
Password
AppleNova Forums » Programmer's Nook »

please explain 'class' in PHP


Register Members List Calendar Search FAQ Posting Guidelines
please explain 'class' in PHP
Thread Tools
nassau
Member
 
Join Date: Jul 2004
 
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?

  quote
staph
Microbial member
 
Join Date: May 2004
Send a message via AIM to 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.
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
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.

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
scratt
Veteran Member
 
Join Date: Jul 2004
Location: M-F: Thailand Weekends : F1 2010 - Various Tracks!
Send a message via Skype™ to scratt 
2006-07-12, 22:29

Quote:
Originally Posted by Brad
Look up "polymorphism" and "inheritance" when you have some time to kill.
Argggghhhhhhhhhh!
  quote
nassau
Member
 
Join Date: Jul 2004
 
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...
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
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.

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
nassau
Member
 
Join Date: Jul 2004
 
2006-07-13, 07:56

thanks brad! and keep it coming everyone!
  quote
Banana
is the next Chiquita
 
Join Date: Feb 2005
 
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?
  quote
chucker
‽
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2006-07-13, 09:55

Quote:
Originally Posted by Banana
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.
  quote
Banana
is the next Chiquita
 
Join Date: Feb 2005
 
2006-07-13, 10:50

Cool- good to know.

Thanks again.
  quote
Mr Beardsley
Member
 
Join Date: Jul 2004
Location: Colorado Springs
Send a message via AIM to 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:
Quote:
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.

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
mooty
Senior Member
 
Join Date: Jan 2005
Location: London, UK
 
2006-07-13, 13:33

Also try this for size:

http://developer.apple.com/documenta...3-CH8-CJBIJAFF

I found it helped my understanding much better...
  quote
chucker
‽
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2006-07-13, 13:36

And for what it's worth, there's another thread about OOP here. Ignore the fact that I had started it and all.
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
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


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 13:10.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova