Jump to content

Archived

This topic is now archived and is closed to further replies.

opitica

difference between function() and class?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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..

[code=php:0]$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[/code]

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..

Share this post


Link to post
Share on other sites
Really... this is like comparing apples to oranges. They are two completely different beasts.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.