whats the purpose for writing classes
hi, im new to classes and was wondering what its purpose can be if i can just write the same thing in a function
The main benifit (and aim) of using classes is that they are self-contained reusable blocks of code. For example, if I wrote a class to generate PDF files, you could then take that class, drop it into your www folder, and do something like:
Also, you could easily extend my PDF-generator class to include fuctions and features of your own without having to alter my original code. Your extended class could then be given to someone else who could use it without having to make a bunch of edits to my code to get it to work.
And finally, keeping your code in nicely organized self-contained classes makes them a lot easier to maintain on large projects :-)
There are many other benifits but these are the most important ones to me.
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 :)
cool thanks for your descriptions it does seem like something i would use now.
whats a easy to understand book you would recommend i purchase to read more on this, i just finished a book by julie meloni called PHP Fast & Easy Web Development, 2nd Edition (Fast & Easy Web Development)
the only php book i own is : Advanced PHP Programming (as recommended by wildhoney and others, its great btw) by George Schlossnagle
but its quite advanced, but a good book i recommend it to anyone :)
|All times are GMT. The time now is 01:58 AM.|
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0