TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-18-2008, 09:45 PM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Smile 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

thanks
sarmenhb is offline  
Reply With Quote
Old 01-18-2008, 10:10 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Hi Sarmenhb,

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:

PHP Code:
<?php

include_once('class_pdf.php');
$pdf = new Pdf();
$pdf->contents('some text here to put in your new PDF file');
$pdf->save('myfile.pdf');
No need to mess about copy/pasting code from my files to yours, or trying to fit the entire PDF generator into a single function.

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.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-18-2008, 10:12 PM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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)
sketchMedia is offline  
Reply With Quote
Old 01-18-2008, 10:34 PM   #4 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

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)

thnx
sarmenhb is offline  
Reply With Quote
Old 01-18-2008, 10:37 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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 :)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:04 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design