whats the purpose for writing classes
View Single Post
01-18-2008, 10:12 PM
Join Date: Oct 2007
Location: Manchester, UK
A lot of people consider classes one of the most difficult things to get your brain around, but when you figure it out you will find it one of the easiest and straightforward things in PHP. The reason that many people don't at first see how it all works is that not everyone has used an OOP language before.
Classes are really just a box, containing variables combined with a set of related functions, from which you then create an object, so its really more like a blueprint or plan of an object, one way to think about it is as a car.
(NOTE: Dont confuse Classes with Objects, they are different. A class is a blueprint for an object, an object is created using the class as a guide, a blueprint for a house for example, a house is an object and it is created using a blueprint aka the class).
A car is a collection of wheels, doors, seats, windows, and so on. Another way to think about it is what a car can do: Speed up (Accelerator), Slow down(Brake), Stop, Park and so on. A class enables you to encapsulate, or bundle these variables and functions into one collection or 'box', this is called a class.
This has a number of advantages as a programmer, as everything you need to know about the car is in one place making development much quicker. Also when you become more advanced with your classes, you will have a premade libary of exsiting 'modules' or classes that you can just pickup and plugin to a new project, saving time by not replicating code. Also they allow the user of the class to have an easy to use API that they can call and not care about how the class works, consider a car's ignition cycle, i havnt the first clue of how it works, but because the ignition system was encapulated withing the car 'class' then all i have to do is turn the key and the rest, the car takes care of (hopefuly, unless its a french car, then it wont start at all, the key is our API in this example as are the foot pedals etc)
hope that makes sense, itskind of a stripped back explantion for now unil you get the idea :)
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
View Public Profile
Send a private message to sketchMedia
Visit sketchMedia's homepage!
Find More Posts by sketchMedia