what is the difference between a function and a class?

Hi,

A Class and Function are VERY different.

Functions
A function is a piece of code that can be called many times, they can do things like calculations without the need to re-write the code a trillion times.

Classes
A class is like a group, it can hold it's own variables and functions. A class cannot just contain code, it is made to hold only functions and variables. Classes are used for the following reasons:
- They group functions and variables
- A class can have it's own properties
- It's good for organisation
- It makes the code look better easier to work with (if done correctly).

Kieran :)

im looking into oop to learn and i know that oop is all classes.

would it be better practice just to create classes instead of functions overall.

trying to improve my programming skills and keep learning more. Cause knowledge is power.. lol

well, Knowledge is power, that works

It really depends on how you like to code. I'm sure there are many advantages but there are also disadvantages.

I don't code everything in classes but I like to keep to them. If a make a file that will be used on all pages on the website I use a class because it organises it well.

It really depends on your style of coding and if you use other languages. I use C++ and some low level languages. For C++ I use Classes so I am used to them.

Kieran :)

you know this is the best help i have gotten in a while here. After someone replies i get no other help.

so thank you.

so from reading from a book earlier, a class can be called once, like lets say at the top of the page, and then everything below can use it?

and my small understanding from a function is that a function is a call once every time you need it?

You can assign a class to a variable. You then use that variable to access the class in the script. You can make more than 1 variable assigned to the class but they are all INDEPENDENT from each over. Meaning that if you change a variable in one of the classes, it doesn't change it in the others(s).

Example:

class Test
{
 var $hi;
};

$test1 = new Test;
$test2 = new Test;

$test1->hi = "Hello";
$test2->hi = "Bye!";

If you use $test1->hi after this, it's still "Hello" and if you use $test2->hi it's still "Bye!"

That's another reason to love classes ;)

You must assign a class to a variable before you use it or you get the following error:

Notice: Undefined variable: test in --***-- on line 2

Notice: Trying to get property of non-object in --***-- on line 2

Or something like that.

Kieran :)

man im starting to love classes. i can just see the possibilities now. man my brain is going wild.

classes just seem so fun.

thanks alot for the help.

i think this posting should be a sticky or something. great info here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.