0

i wanted to know the difference between having a collection of functions i could create in a PHP file compared to a PHP class file?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Ryan K
  • 346
  • 4
  • 15
  • Functions inside a class are called methods and outside a class they are just functions ^.^ A bunch of functions have different goals but in a class they can be used for mainly one goal. For example: phpmailer class: mailing, form class: building forms, and more. – Gilly Dec 10 '12 at 16:55

5 Answers5

3

for practical purposes, the CLASS is different because it can be instantiated. When it is instantiated it will have its own unique properties and values.

In this way you can have the same class instantiated multiple times each with their unique properties.

a common example of this would be a character in a game; i.e. you have a class called enemy. you produce 5 enemies by instantiating the class that many times. Each will have their own properties, life, speed, etc... you can access each of the instantiations individually and run methods within that adjust their properties.

NappingRabbit
  • 1,888
  • 1
  • 13
  • 18
2

A class is a collection of data and functions that can be instantiated (a different set of data for each instance).

Classes are a focal point of OOP, and they exist to organize code with a focus on objects. OOP better facilitates modularity than imperative programming. For your research, it would be better to look at this answer to a different question and the link contained therein.

Community
  • 1
  • 1
weberc2
  • 7,423
  • 4
  • 41
  • 57
2

Functions that are defined within a class construct are referred to as methods, which typically require an instance of that class to work (with the exception of static methods). Such as instance would keep "shared" data between the methods to work with, encapsulating the behaviour of an object.

A collection of functions can do this as well, for example fopen() opens a stream that can be passed to fread(), fwrite(), etc. as their first argument; a similar approach can be made with a stream class, for example:

$f = new MyFile('filename');
$f->read(10);
$f->write('hello');

As opposed to:

$f = fopen('filename');
fread($f, 10);
fwrite($f, 'hello');
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

A collection of functions is just a random collection of functions. A class encapsulates a specific area of behaviour.

For example, when you are adding a function to a class, you should be thinking "is this the correct and only place for this?"

It is possible to write a class in PHP that fits both of these definitions - but you should aim for the latter.

Fenton
  • 241,084
  • 71
  • 387
  • 401
-1

I think you could imagine the difference between functions and classes in PHP as the difference between the languages C and C++. C was made first and then C++ followed as its Object Oriented version. So is PHP, it was made first based on C, as a simplified C for Web (Personal Home Page) and later added OOP functionality. But the beauty of PHP is in its simplicity, you can make a whole new page in a couple minutes without having any solid programming experience. Using classes in PHP kind of break this purpose as it can overkill if you just develop something relatively simple.

Classes are great for libraries. I suggest you using them. And it's a more advance coding style: creating classes means creating your own libraries which you can use for different projects. Just make sure you don't overuse them. Not for everything in PHP really needs a class, just for those forever-repeating functionalities. Classes add the flexibility and scalability to your projects.

MAXIM
  • 1,223
  • 1
  • 9
  • 16