Jump to content


Photo

difference between function() and class?


  • Please log in to reply
3 replies to this topic

#1 opitica

opitica
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 15 July 2006 - 05:53 AM

What is the big difference between function(){} and class, it seems like I could create a file filled with a bunch of function(){}'s and it would still serve the same purpose as a page full of classes except for the way you call them. I see lots of sample code that have a class filled with lots of function(){}'s and not the other way around.

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 15 July 2006 - 06:47 AM

You could say that a class is just a group of functions, but really it's so much more than that.

Think of a class as a group of functions that are used to manipulate the same amount of data.

Say you pull a user from the database. You would have to assign many variables accross multiple pages if you had any sort of complexity to your member system.

An easier way would be..

$user = new user(2)[/php)  (assuming user is the class, and 2 is the userid it pulls from the databse, your contructor function would populate all of the users information, which is easily grouped together and used like...

[code=php:0]print "Hello ". $user->firstName . " welcome to thiswebsite. Your last login was " . $user->lastLogin

It's really a small and silly example, but if you let yourself think a little at the possibilities (and this isnt the only one, polymorphism is another GREAT feature of classes)

I hope this helped shed some light..

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 July 2006 - 07:30 AM

Really... this is like comparing apples to oranges. They are two completely different beasts.

#4 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 15 July 2006 - 08:34 AM

If you are a beginner you use function. //structued programming
An advance programmer use CLASS. //Object Oriented programming

A class may compose of variables, functions and etc.

Class make you programming logic very organize.
If you want full bloom OOP languages you use C++ or Java.

Please take note OOP in PHP is not full bloom yet.
But don't worry they are ways to make it full bloom,
just go ahead and study CLASS and OOP topics.
I'm sure you will find the hiden treasure. :)

enjoy the info!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users