Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/14654-difference-between-function-and-class/
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..
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!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.